开发者

How do you interrupt a process after starting it with exec()?

开发者 https://www.devze.com 2023-02-28 02:44 出处:网络
If I start a process with exec(), how can I later terminate that process, with say pressing/sending the \"q\" key.Right now, when I execut开发者_如何转开发e the process, PHP will hang until it finishe

If I start a process with exec(), how can I later terminate that process, with say pressing/sending the "q" key. Right now, when I execut开发者_如何转开发e the process, PHP will hang until it finishes and returns.


function PsExec($commandJob) { 

    $command = $commandJob.' > /dev/null 2>&1 & echo $!'; 
    exec($command ,$op); 
    $pid = (int)$op[0]; 

    if($pid!="") return $pid; 

    return false; 
} 

later on... exec("kill -9 $pid", $output);


if you want your script to exit regardless of wheter the exec is done, you can redirect the out put to another file,

exec("php dothis.php >> file.log");

i hope that helps you


You could use pcntl_fork and pcntl_exec to launch your program and posix_kill to terminate it.


If I start a process with exec(), how can I later terminate that process, with say pressing/sending the "q" key.

Instead of using exec, you could look at using proc_open, which requires that you pass in an array specifying three streams -- one for stdin, one for stdout and one for stderr.

This will let you easily feed input into the program while processing output, without blocking just waiting for it to execute. You can later use proc_terminate to viciously murder it, if needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消