How to setup Cron jobs to execute a php file on your server without problems or errors

cron jobs in control panel run php

Trying to run a chron job on your server which executes a php file? You might encounter any number of errors. When I tried running a php file with cron jobs to run feedgater for a joomla install I followed the instructions but got errors like "No such file or directory" and "Could not open input file:". Here is some support info great for anyone having problems running php files through a chron job.



First I'll start by letting you know about different chron job command line instructions and what they mean:

When I tried installing feedgater on Joomla, the chron job command line I was instructed to use was:

*/30 * * * * /usr/local/bin/php /var/www/your.web.site/htdocs/administrator/components/com_feedgator/cron.feedgator.php >> /dev/null

but after editing it to include my path it didnt work and nothing at all happened. No feedback nada. The first thing I had to do was delete the bit " >> /dev/null" this tells cron jobs not to send an email when the cron job is complete. This did not help as I had no idea what problem cron was encountering. So I deleted it.

I also took the time to change the frequency. The bit at the beginning:
"*/30 * * * *" tells cron to exectute the php file every half hour. Too long for testing. Change it to:
"* * * * *" (without the quotes) the five stars will tell cron jobs to run the php every minute. Thats better.

Finally I started getting email logs of what what happening with cron.

The first was an error: "No such file or directory". Obvously a path/filename problem. To fix it I did two things:

1) I checked the bit "/usr/local/bin/php", my web host told me to use "/usr/bin/php" but obviously that was wrong. I changed it back to was was originally instructed.
2) I put the part "-q" at the end. As I understand it (I'm no expert) but this allows cron to run the php file in it's relative path. In otherwords it can help get rid of some path problems with cron. Good enough for me.

Now the message I started to get was "Could not open input file:". This turned ot to be an incorrect path in the cron command line. I simply missed out a folder. Make sure you painstaking check the path "/var/www/your.web.site/htdocs/administrator/components/com_feedgator/cron.feedgator.php" (change it of course to reflect the path of your php file on the server).

In the end the command line I used in cron was this:
*/30 * * * * /usr/local/bin/php /home1/DOMAIN/public_html/FOLDER/administrator/components/com_feedgator/cron.feedgator.php -q -f

The two parts in capitals are my main domain and a folder in which an addon domain sits.

You'll also notice that I changed back to once every half hour and also added at the en -f. In laymans term, this just means if an important line at the start of the php isn't there, it won't stop and produce and error. I worked for me so I hope you have less problems running your own php files using cron jobs with that advice.


Posted in Joomla