Skip to content

Instantly share code, notes, and snippets.

@ynaoto
Last active December 14, 2015 22:29
Show Gist options
  • Save ynaoto/5159127 to your computer and use it in GitHub Desktop.
Save ynaoto/5159127 to your computer and use it in GitHub Desktop.
Command launch process with watching the possible error at launching stage.
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