Last active
December 14, 2015 22:29
-
-
Save ynaoto/5159127 to your computer and use it in GitHub Desktop.
Command launch process with watching the possible error at launching stage.
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
function spawn($command_line, $dir) | |
{ | |
$descriptorspec = [ | |
0 => [ "pipe", "r" ], // stdin | |
1 => [ "pipe", "w" ], // stdout | |
2 => [ "pipe", "w" ], // stderr | |
]; | |
$process = proc_open($command_line, $descriptorspec, $pipes, $dir); | |
if (!is_resource($process)) { | |
return null; | |
} | |
stream_set_blocking($pipes[2], 0); // set non-blocking mode for stderr | |
usleep(10000); // wait for 10ms. this dirty hacking is enough for this application | |
$ps = proc_get_status($process); | |
if (!$ps["running"]) { | |
return null; // proccess could not survive | |
} | |
return [ $process, $pipes ]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment