Debugging SugarCRM Scheduled Tasks

Debugging PHP is best done using an IDE (in my case Eclipse) where you can set break points and step through code and look at variables. Normal web applications are straight forward normally but scheduled task debugging is a bit of a challenge. Below are my suggestions as to how best this can be done.

Before starting I must say, don’t try this on a production server. You will be affecting the running of normal scheduled processes if you do!

You will need to make sure that the scheduled task you are debugging is the only task that will run. To achieve this, set all other job as deleted as follows (keep a note of what is active and what has previously been deleted before you start).

UPDATE job_queue SET deleted = 1
UPDATE job_queue SET deleted = 0, execute_time = '2017-01-01 00:00:00', status = 'queued' WHERE id = ''

As the execution time set is earlier than now, the job will be run.

Now to start debugging. You need to debug cron.php in the root directory, as a command line process. If you try defining it as a web application in your IDE (ie. in a browser) it will die before doing anything.

Break at this line in cron.php


Then step in

In runCycle() you will see a loop through the scheduled tasks, like this.

for($count=0;$count<$this->max_jobs;$count++) {
$this->job = $this->queue->nextJob($myid);
if(empty($this->job)) {
if(time() >= $cutoff) {

All you need do is break on executeJob() and have a look at $this->job to see if it is the one you are interested in. It should be as you have stopped all others.

Step into it and see what its doing.

Within executeJob() you find the following

$res = $this->job->runJob();

You should step into this.

The line that does all the work is this line.

$res = call_user_func_array($func, $data);

Step into that and you are in the code you want to debug!

When you have finished debugging, remember to reinstate the deleted status of all the other jobs.

If you think this can be improved, please let me know