Skip to content

Instantly share code, notes, and snippets.

@reconbot
Created June 29, 2011 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reconbot/1052561 to your computer and use it in GitHub Desktop.
Save reconbot/1052561 to your computer and use it in GitHub Desktop.
daemontaks
<?php
// http://bakery.cakephp.org/articles/reconbot/2008/08/26/daemontask
class DaemonTask extends Shell {
function execute($prefix = '') {
//the key the pid is stored with - default to just 'pid'
$pidstring = $prefix . 'pid';
if(!Cache::read($pidstring)){
Cache::write($pidstring, getmypid(), 3600);
}else{
$ps = shell_exec('ps -o pid -A');
$ps = explode("\n", trim($ps));
foreach($ps as $key => $value){
$ps[$key] = trim($value);
}
if(in_array(Cache::read($pidstring), $ps)){
exit("already got a process running\n");
}else{
echo "replacing stale pid\n";
Cache::write($pidstring, getmypid(), 3600);
}
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment