Created
January 30, 2018 11:01
-
-
Save chluehr/ddabc14e2b3eb614efb4c2a7a2b75188 to your computer and use it in GitHub Desktop.
Spawn a shell command from PHP, check Process status, respawn if not running.
This file contains hidden or 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 | |
/** | |
* $ php p.php | |
* S SPAWNED! RRRRS SPAWNED! RRRRRS SPAWNED! R | |
*/ | |
function spawn() { | |
$descriptorspec = array( | |
0 => array("pipe", "r"), // stdin | |
1 => array("pipe", "w"), // stdout | |
2 => array("pipe", "w") // stderr ?? instead of a file | |
); | |
$process = proc_open('bash -c "sleep 5s"', $descriptorspec, $pipes); | |
echo " SPAWNED! "; | |
return $process; | |
} | |
function isRunning($process) { | |
if (!is_resource($process)) { | |
return false; | |
} | |
$status = proc_get_status($process); | |
return ($status["running"]); | |
} | |
$process = null; | |
while (true) { | |
sleep(1); | |
if (isRunning($process)) { | |
echo "R"; | |
} else { | |
echo "S"; | |
$process = spawn(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment