Quick Cron Job Test

Here's a couple steps that will help your to quickly determine whether the tasks you're setting up in crontab are actually running.

  1. Create a blank text document named cron-test.txt and upload it into your test folder. Change the CHMOD settings on it to 777.
  2. Create a file named cron-test.php with the following code in the same folder:
    <?php
    $crontext = "Cron Run at ".date("r")." by ".$_SERVER['USER']."\n" ;
    $folder = substr($_SERVER['SCRIPT_FILENAME'],0,strrpos($_SERVER['SCRIPT_FILENAME'],"/")+1);
    $filename = $folder."cron-test.txt" ;
    $fp = fopen($filename,"a") or die("Open error!");
    fwrite($fp, $crontext) or die("Write error!");
    fclose($fp);
    echo "Wrote to ".$filename."\n\n" ;
    ?>
    
  3. Finally, enter the following line into your crontab file:
    * * * * * php /yourfolder/cron-test.php
    
  4. Wait a couple minutes and then open up cron-test.txt and see what's there.

Comments

Loading…

This post was published on December 15th, 2008 by Robert James Reese in the following categories: cron jobs and PHP. Before using any of the code or other content in this post, you must read and agree to our terms of use.