简体   繁体   中英

How to debug cron job - codeigniter 3

My cron job is triggering properly from a godaddy shared server:

wget `http://www.domain/APP/index.php?/cron_jobs/cron_job_TEST`

There are no errors returned in the cron result:

HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: `index.php?%2Fcron_jobs%2Fcron_job_TEST'

I set the test to write to a text file - and also to write to a database table.

When I test this - by visiting the URL - http://www.domain/APP/index.php?/cron_jobs/cron_job_TEST The text file and table are written to properly.

But when the cron runs - there is no result to the text file or the database table.

CONTROLLER/FUNTION 'cron_jobs/cron_job_TEST':

// CRON_1
    public function cron_job_TEST(){

        file_put_contents("test_cron_job.txt", "cron_job_TEST-> ".date('l jS \of F Y h:i:s A') . "\n", FILE_APPEND);


        $this->db->query("INSERT INTO `TEST_CRON` (`ID`, `DATE`) VALUES ('', NOW());");


    }// END CRON_1

I have also tried the /web/cgi-bin/php5 $HOME/html/ methods without luck. I thought the point of the wget was that it would behave just as though you are visiting the URL.

What could be going wrong here?

How do I debug this?

Rather than using wget to fetch your file from a web server, you should execute the command directly from the command line by invoking PHP on your codeigniter's index.php file:

php /path/to/your/project/html/index.php cron_jobs/cron_job_TEST

If you execute your codeigniter app this way, you should be able to see the output and also the errors that might bubble up during its execution.

I'd also suggest writing a log file from your cron method so you can output values and, if you are clever, be sure which branches of your code are executing.

EDIT: you might also alter your cron job, whichever method you choose, to route the output of the cron job into a file so you might have some clue about what went down:

wget `http://www.domain/APP/index.php?/cron_jobs/cron_job_TEST` > /path/to/file.txt

EDIT: I think you can also route both stderr and stdout to the same file like so

wget `http://www.domain/APP/index.php?/cron_jobs/cron_job_TEST` &> /path/to/file.txt

Obviously, the user that owns this cron job must have permission to write that output file if the file already exists or must have permission to write the directory if the file does not exist.

Once the cron job has run, you might be able to look in this file and get a better idea of what transpired.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM