我对子进程有问题,有时即使出现错误也会以代码0退出。作为一种最简单的解决方案,我想检查控制台上显示的消息是否包含“错误”短语,如果是,我想使用-1错误代码杀死子进程-因此mustRun()方法将引发ProcessFailedException异常。
php 7.1,symfony进程^ 4.2
$command = 'the_subprocesses_command';
$path = 'example_path';
$process = new Process($command, $path);
$input = new InputStream();
$process
->setIdleTimeout($timeout)
->setTimeout($timeout)
->setInput($input)
->setTty(true);
$process->mustRun(function ($type, $buffer){
echo $buffer;
if(stripos($buffer,'error')){ //dummy check if string contains error appears
echo "\nError occured\n";
exit(-1); // kill child process with -1 exit code
}
});
我希望在回调的$ buffer上发现错误短语时,子进程的输出会被-1杀死。