0

I am stuck in a cron script and need your help.

My site is hosted on 1and1 server. I have written a cron script in php using class function structure.

There are two scripts actually. Those are: 1) init.php 2) job.php

In init.php i have written the necessary class and functions. In job.php script, i have included the init.php at the top and below to this i have written other functionality (database query and sending of emails).

If i execute the script directly using the browser (http://www.abc.com/job.php), the script is executing fine. I am getting emails. But, i am not getting any response after setting this script as a cron task using shell. I have no idea how to check the error log of the cron as well.

Sample structure of the php file is given below:

init.php:

define('DB_SERVER','***********');
define('DB_USERNAME','*******');
define('DB_PASSWARD','******');
define('DB_DATABASE','*******');


class information{

   public function __construct(){
        $connection = mysql_connect(DB_SERVER,DB_USERNAME,DB_PASSWARD) or die ('connection error'.mysql_error());
        mysql_select_db(DB_DATABASE,$connection) or die('database error'.mysql_error());
   }

   public function configuration(){

        $sql = "SELECT * FROM config order by id";
        $exe = mysql_query($sql);

        $site_config = array();

        while($res = mysql_fetch_array($exe)){      
            $site_config[$res['name']]=$res['value'];
        }
        return $site_config;
    }

}

$obj = new information;

job.php:

require('init.php');

class Achdirect_cmi
{
    private $ID;
    private $AID;
    private $Key;

    public $site_config;

    public function __construct()
    {
        global $obj;

        $this->site_config = $obj->configuration();


        $this->ID           = $this->site_config['id']; 
        $this->AID           = $this->site_config['A_id'];
        $this->Key = $this->site_config['key'];     

    }
.
.
.
.

}

Please help me to execute this script.

**The cron job is setup using SSH access.

Below mentioned command has been used to to set the cron script.

          • /usr/bin/php /a/b/htdocs/scripts/job.php

To check whether my shell command and configuration executes or not, i have added a test script as well. The test script executed properly.**

After analyzing the functionality one by one, i have found that the below code is not executing. Variables have been initialized properly but not creating the object of "SoapClient". Please note, the class SoapClient is not defined by me, but its a core functionality of php5.

try{

$client = new SoapClient($url, array("location" => $location));

$params = array("ticket" => $auth_array, "MerchantID" => $MerchantID, "TransactionID" => $trace_number );

$response = $client->getTransaction($params);

}
catch (Exception $e){ 

echo $e->getMessage();
}

Please suggest me the needful.

Debashis
  • 566
  • 2
  • 14
  • 34
  • 1
    Add `MAILTO=me@example.org` in your crontab file; this will send an email if any of the cron jobs outputs something – Ja͢ck Jun 26 '12 at 05:19
  • 1
    How is the cronjob setup? Do you have SSH access to it? Or it's web-based console only? – Roman Newaza Jun 26 '12 at 05:19
  • I have just mentioned the sample code structure above, In my original script mail is included. If i execute the script using browser, i am getting emails. But after setting this script as a cron script, i am not getting emails. I have mentioned this point above as well. The cron job is setup using shell access and command. I have also tested using a test script to check whether cron works or not using the same shell configuration. – Debashis Jun 26 '12 at 05:31
  • Dear Roman Newaza, I have SSH access only to set the cron job. – Debashis Jun 26 '12 at 05:47
  • * * * * * /usr/bin/php /htdocs/scripts/job.php – Debashis Jun 26 '12 at 05:49
  • If visiting the URL works, then that means that the problem lies within the cron script, not within the php files. Can you provide the script details? What program are you trying to run? In my experience, you could run `curl`, or `wget`. – andresuribe Jun 26 '12 at 05:20
  • Dear andresuribe, i am not understanding what do you wanted to know by 'what program are you trying to run'. Basically, my script will be used to get transaction details from other server and update my database. The code is written in PHP. – Debashis Jun 26 '12 at 05:45
  • You use cron to schedule a job that will run a command or a shell script. What are you scheduling? Command or shell script? If your scheduling a command, what command are you scheduling? If your scheduling a shell script, what are the shell script contents? – andresuribe Jun 26 '12 at 05:49
  • I am scheduling a shell script. The script is written in php. I have added other necessary information at bottom of my original post. Please have a look into it. – Debashis Jun 26 '12 at 06:05

1 Answers1

0

Your problem is most likely the "require('init.php');"

The cron job runs from where the cron is located, not from the path you have the job.php file. So if you want to include the file then you should include the full path the the include file.

require('/a/b/htdocs/scripts/init.php');

(If that is the correct directory - seems an interesting naming structure!)

Robbie
  • 17,605
  • 4
  • 35
  • 72
  • I have set the corrected directory path as you have suggested. Yes, the given directory path is not a valid structure. I have also made another file(check.php) and copied all the functions of init.php and job.php into check.php file and set another cron task. The file is executing using direct access but not executing using cron task. – Debashis Jun 26 '12 at 08:33
  • After analyzing the functionality one by one, i have found that the below code is not executing. Variables have been initialized properly but not creating the object of "SoapClient". Please note, the class SoapClient is not defined by me, but its a core functionality of php5. try{ $client = new SoapClient($url, array("location" => $location)); $params = array("ticket" => $auth_array, "MerchantID" => $MerchantID, "TransactionID" => $trace_number ); $response = $client->getTransaction($params); }catch (Exception $e){ echo $e->getMessage(); } Please suggest me the needful. – Debashis Jun 26 '12 at 08:53