Created
June 29, 2011 00:16
-
-
Save reconbot/1052561 to your computer and use it in GitHub Desktop.
daemontaks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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