Any cron experts w/debugging tips?

I’m trying to figure out why cron is giving me false positives as I try to learn cron.

I have a test script that writes the time to a file and echos the time and how many characters were written to the file. Once that works, I plan to use more dynamic data. Note - the file, newtime.txt has permissions 777.

basically the concept is (not actual code):
$test = date(“d/m/y : H:i:s”, time());
$file = fopen(“newtime.txt”,“w”);
$result = fwrite($file,$test);
echo "wrote: “.$result.”, ".$test;

I setup the cron in the DH panel, and it runs and emails me the correct output, which is the number of bytes written and the time that the cron job was run. So the the system finds the php & runs the cron job w/o a problem and has a return value for fwrite > 0, suggesting that the file was written, and the correct text - the time in the email matches the time the cron job was run.

The cron looks like:
/usr/local/php5/bin/php /home/<my_name>/

BUT, here’s where some guidance would help, the file is not actually written. I can access the file newtime.txt via ftp or ssh, and the file is not updated. WHY would fwrite return a value if the file was not written?

So, from the above it seems that cron has no problem finding and executing the php script as evidenced by the correct output in email and the semblance of the correct number of bytes written by fwrite.

How can I test what’s going on? The result is the same if I place:
#!/usr/local/bin/php -q

above the <?php

You may also wonder if it works if I use a browser. It does - both the screen output is correct, AND the file is correctly updated. Recall that the cron was already doing everything except writing the file earlier. There should be no directory problem.

To make matters more confusing, if I indeed ‘cd’ to the directory of the script, the cron works as it did before, outputing the same value for fwrite, AND also updating the file.

If cron found the script and executed before, why does using ‘cd’ matter? Why is fwrite returning the correct value for bytes written, even when the file was not written? What debugging flags can I use to figure out what’s happening?

Inquiring minds want to know.

I suggest you try writing the full filename in the fopen … fopen("/home/…etc…

The file is most likely being written to your home directory (/home/<my_name>/newtime.txt), for two reasons:

  • Running PHP from command-line is different from running it via the web server in several way. In particular: In the web server environment, the working directory is automatically set to the script’s directory. On the command-line, PHP uses whatever the current working directory is at the time.

  • Cron jobs are always run with the current directory set to your home directory.

One way to avoid differences between command-line and web-server PHP, is to have cron preform a GET on the script directly, just as if you loaded the page with a browser. Here’s how to do it using the Curl tool:


Alternatively, you could use an absolute path to the file (as erikjacobsen suggested), or set the working directory in the cron job:

cd ; /usr/local/php5/bin/php crontest.php

I hope that helps.

So the file was indeed being written to my home directory, hence the fwrite success return value. Using the ‘cd’ command as I had done based on other suggestion or erikjacobsen’s solution both solve the problem. Now I know why.

Thank you. I think I have a much better foundation of what the heck is going on w/ Cron vs. command line, vs. web access.