Cron jobs seem to be one of the things I have to do often that give me trouble. That’s why I’m putting together this guide full of troubleshooting tips for people having problems with cron jobs.
Just today, for example, I wasted several hours figuring out why my cron job didn’t seem to be working and here is what happened:
I set up my cron job exactly as I always do according to my own tutorial I posted here:
however, it didn’t seem to be working so I tried chaing the shebang, changing the php location, etc. with no luck at all.
Finally after hours of wasting time, I figured out that the output of my cron job was not going where I thought it would go. For example, my test script created a file and wrote a line to that file to show the cron job ran. My cron test PHP file looked like this:
$myFile = “cronresults.txt”;
$fh2 = fopen($myFile, ‘a’) or die(“can’t open file to append”);
$stringData = “appended text from cron job…\n”;
Simple, right? well the trick is the $myFile variable works as expected in that it creates the file in the same directory as crontest.php when crontest is called from a web browser. So when I opened up Firefox and went to mysite.com/crontest.php, it worked fine, it would create a file named cronresults.txt in my public_html folder and write a simple line of text to it. However. HERE IS THE TRICKY PART! I call the script in crontest.php from a cron tab by adding the following line in crontab -e:
* * * * * /usr/bin/php /var/www/mysite.com/public_html/crontest.php
but when I call it that way and look for the cronresults.txt file or look for the line of text it added to the file if it already exists, there is nothing so it appears as if the cron job didn’t work! Oh damn! I say. Then I try everything I can think of to fix it. The problem is that is was never broken! When you call a PHP script from a cron job and you do not specify the file path of the results file, it assumes the current users home folder! Therefore, it was working the whole time, but was creating and writing to a file in my /home/root directory because that’s the directory I called crontab -e from! THerefore my solution was as simply as replacing the $myFile line with this:
$myFile = “/var/www/mysite.com/public_html/cronresults.txt”;
WHEN YOU USE THE ENTIRE PATH IT WORKS AS EXPECTED! I can’t over emphasize the importance of this because I wasited several hours due to not knowing this.
Cron Job Troubleshooting Tips
- Always use a simple test script to test cron jobs for the first time on a new server. You can use my example crontest.php script above, but be careful to set the $myFile variable with the entire path to the output file or you’ll have the same problem I wrote about above in this post.
- Always use a very simple command line in crontab when first testing your cron job. You can customize it after you get it working. I use something like: * * * * * /usr/bin/php /path/to/cron/crontest.php then crontest.php will be executed every minute so you can quickly see if it is working. This makes testing and debugging much easier.
- Can’t figure out your Shebang or don’t know where PHP is on your server? Then simply go to a command prompt on your server using putty remotely or any command prompt if you are on your own server and type the following command: “which php” without the quotes. That will give you the path to php for both your shebang and your crontab file, like where it says /usr/bin/php in my examples above.