When running PHP from cron, I've always thought its a good idea to create a lockfile, to prevent the script from running twice. For example a cron that is ran every minute, might occasionally take longer than a minute to complete. Meanwhile, you might occasionally need to override the lockfile, for example the script failed midway, and never removed the lockfile. To help with those scenerios, here is what I call my PHP cron template. I hope you find it useful.
| Code: |
#!/usr/bin/php -q
<?php
// config
$lockfile = './'. $PHP_SELF .'.lock'; // lockfile name
//how many minutes to just sit there before removing a stale lockfile and trying again
//in case of failure on prior run
$hardstart = 5;
// end config
//////////////////////////////////////////
//is lockfile stale ?
$seconds = $hardstart * 60;
if (file_exists("$lockfile")){
if (file_exists("$lockfile") && ((time() - filemtime("$lockfile")) > $seconds)) {
if (unlink("$lockfile")){
$del = $del + 1;
echo "Deleted: $lockfile ";
}
} else {
echo "\n".'last batch is still running. Exiting '."\n"; die();
}
} else {
// create lockfile and start fetching
$handle = fopen("$lockfile", "w");
$stamp = date("YmdHis");
fwrite($handle , $stamp);
fclose($handle);
// put the rest of your script here
// remove the lockfile just before exiting ...
unlink("$lockfile");
}
// die() is preferable to exit() on command line scripts, while very similar
// die() halts processing and immdediatly frees the resources, exit() actually
// continues to be parsed, and only frees resources afterwards.
die();
?>
|