Last active
January 3, 2016 20:49
-
-
Save blamh/8517811 to your computer and use it in GitHub Desktop.
PHP Class for running a script as daemon in Linux. The script is started in the background by itself, to stop the daemon use filename.xphp --stop.
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 | |
/** | |
* Class for running a script as daemon in Linux. The script is started | |
* in the background by itself, to stop the daemon use filename.xphp --stop. | |
* | |
* $d = new Library_Cmd_Daemon('daemon.pid'); | |
* $d->run(function() { | |
* while(1) { | |
* sleep(10); | |
* } | |
* }); | |
* | |
*/ | |
class Library_Cmd_Daemon | |
{ | |
/** | |
* File path for the pid file- | |
* | |
* @var string | |
*/ | |
private $pid_file; | |
/** | |
* The Process ID. | |
* | |
* @var int | |
*/ | |
private $pid; | |
/** | |
* Flag to indicate if the process is in daemon mode | |
*/ | |
private $daemon_mode = false; | |
/** | |
* Constructor | |
* | |
* @return void | |
*/ | |
function __construct($pid_file) | |
{ | |
$this->pid_file = $pid_file; | |
} | |
/** | |
* Destructor | |
* | |
* @return void | |
*/ | |
function __destruct() | |
{ | |
if ($this->daemon_mode) { | |
$this->clearPID(); | |
} | |
} | |
/** | |
* Method for running the background code. | |
* | |
* @param Closure $func The code to be run. | |
* | |
* @return void | |
*/ | |
function run($func) | |
{ | |
$bgdaemon = $stop = false; | |
foreach ($_SERVER['argv'] as $e) { | |
switch ($e) { | |
case '--bgdaemon': | |
$bgdaemon = true; | |
break; | |
case '--stop': | |
$stop = true; | |
break; | |
case '--status': | |
print $this->isRunning() ? | |
"The daemon is running\n" : "The daemon is not running.\n"; | |
return; | |
case '--help': | |
print "Available parameters are: --status and --stop.\n"; | |
return; | |
} | |
} | |
// Check that the process is running | |
if (!$this->isRunning()) { | |
$this->clearPID(); | |
} | |
// Kill the process | |
if ($stop) { | |
$this->stop(); | |
return; | |
} | |
// Background daemon stuff. | |
if ($bgdaemon) { | |
$this->daemon_mode = true; | |
// Save the pid for later use. | |
file_put_contents($this->pid_file, getmypid()); | |
$func(); | |
return; | |
} | |
$args = array(); | |
foreach ($_SERVER['argv'] as $k => $v) { | |
// Don't include the filename. | |
if ($k === 0) { | |
continue; | |
} | |
$args[] = $v; | |
} | |
$args_c = implode(' ', $args); | |
// daemonize | |
if (!$this->isRunning()) { | |
system("({$_SERVER["SCRIPT_NAME"]} $args_c --bgdaemon) >/dev/null &"); | |
} | |
} | |
/** | |
* Detects if the process is running or not. | |
* | |
* @return bool | |
*/ | |
public function isRunning() | |
{ | |
$pid = (int)@file_get_contents($this->pid_file); | |
if ($pid === 0) { | |
return false; | |
} | |
if (posix_getsid($pid) === false) { | |
return false; | |
} | |
if (!file_exists("/proc/$pid/")) { | |
return false; | |
} | |
$this->pid = $pid; | |
return true; | |
} | |
/** | |
* Clears all the PID information from the obj and the disk. | |
* | |
* @return void | |
*/ | |
private function clearPID() | |
{ | |
@unlink($this->pid_file); | |
$this->pid = 0; | |
} | |
/** | |
* Method for stopping the running daemon. | |
* | |
* @return void | |
*/ | |
private function stop() | |
{ | |
if ($this->pid === 0) { | |
return; | |
} | |
posix_kill($this->pid, 9); | |
$this->clearPID(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment