Skip to content

Instantly share code, notes, and snippets.

@chluehr
Created January 30, 2018 11:01
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 chluehr/ddabc14e2b3eb614efb4c2a7a2b75188 to your computer and use it in GitHub Desktop.
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.
<?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