My experiences of changing php cron job running by wget to php-cli /usr/bin/php

tool used to convert the cron interval to human readable time: https://crontab.guru/

This is one of my real world experiences.

Lately we migrated some websites with the entire WHM and all the cron jobs running as is.

There were about 12 cron jobs running, something like below:

59 23 * * 6 /usr/bin/wget -O /dev/null “http://www.somedomain.net.au/some-api-data.php” >/dev/null 2>&1

However because of the domain and IP routing changes, we could no longer run wget for any of the php files under these domains.

So my alternative is to move these script running via wget to internally running via php-cli.

Below is the approach:

Firstly created a test cron.

SSH as root, put a small php script to output the time stamp and run as cronjob every minute as below. ( crontab -l will show this line at the bottom)
* * * * * /usr/bin/php /home/website/public_html/date_cron_test.php >> /usr/logs/date_cron_test.log 2> /dev/null
Successfully generates the log file and had correct record for outputs of each round.

Then change all the cron jobs like below

59 23 * * 6 /usr/bin/wget -O /dev/null “http://www.somedomain.net.au/some-api-data.php” >/dev/null 2>&1

to

59 23 * * 6 /usr/bin/php /home/somedomain/public_html/some-api-data.php >> /usr/logs/some-api-data.log 2> /dev/null

Change all the old wget ones to the /usr/bin/php php-cli ones accordingly.

Also the php needs to be set mode with x as executable, eg 755, this depends on your actual server settings.

Finally run service crond reload

It’s all set up. All we need to do is checking the cron and php log later to verify.

 

References:

  • Cron job executing php trouble shooting: http://stackoverflow.com/questions/7397469/why-is-crontab-not-executing-my-php-script
  • crontab php file and output to log file result (append mode): http://stackoverflow.com/questions/9456424/crontab-php-file-and-output-to-log-file-result
  • Meanings of 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>&1: http://unix.stackexchange.com/questions/70963/difference-between-2-2-dev-null-dev-null-and-dev-null-21
  • cron “BAD FILE MODE”: https://www.redhat.com/archives/rhl-list/2005-February/msg02458.html

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.