php stop shell exec on cancel
When I开发者_开发百科 cancel a PHP Script by pressing the browser's "Stop"-Button, it seems that an external binary run by exec(..)
is not stopped. If the server is configured to not handle more then one script per user, that disables any user activity until the runned command is finished. How cann I tell PHP to kill the executed process when canceled?
In short, with an element of difficulty.
Your browser says to the server "Do this page", server then replies with what it got back when it ran the script. If the client goes away, the server usually still completes the script, irrelvant of wether the browser listens or not.
Only way I can think of is, if the server is UNIX, you can possibly do something to try and work out the PID of your running command, and if you read http://php.net/manual/en/features.connection-handling.php you can set things to happen on disconnects. Following on from that you could then having a PID run another command to kill off the command.
However, I wouldnt like to guarentee results.
$command = 'your command' . ' > /dev/null 2>&1 & echo $!';
exec($command, $output);
echo $pid = (int)$output[0];
Then when you want to kill it:
exec("kill -9 $pid");
in webpage use exec to lunch process and hold on to the returned pid.
$pid = exec($command);
later in your UI check if process ended and if needed allow user to kill process with something like:
exec("kill -9 $pid");
Since it's started by Apache, Apache should be able to kill it. I see ajax calls to some sort of REST API, and you should take care not to allow users to kill more then the process they started.
精彩评论