如何用php杀死正在运行的bash文件

时间:2015-07-14 13:39:46

标签: php bash

我有两个按钮"开始采集"和#34;停止收购",

开始按钮执行bash文件并且工作正常:

<form action="Control.php" method="post">
<input value="Continous Acquisition " name="Continuous" type="submit">
</form>

<?php
if (isset($_POST['Continous'])) {
shell_exec('sh /Desktop/run_test.sh');
}
?>

我不知道如何在按下“停止”按钮时停止执行

<form action="Control.php" method="post">

<input value="Stop Acquisition " name="Stop" type="submit">

 </form>

任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

当您使用shell_exec时,它将同步运行并等待脚本完成。相反,您可能希望使用proc_open()proc_close()来执行此类操作:

$descriptorspec = array(
    0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
    1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
    2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);

$cwd = '/tmp';

$process = proc_open('sh /Desktop/run_test.sh', $descriptorspec, $pipes);
if (is_resource($process))
{
    // We have a running process. We can now get the PID
    $info = proc_get_status($process);

    // Store PID in session to later kill it
    $_SESSION['current_pid'] = $info['pid'];
} 

将PID存储在会话中(或文件或任何您想要保存的位置)后,您可以使用systemexec来运行kill -9 $pid命令。< / p>

参考文献:

http://php.net/manual/en/function.proc-open.php http://php.net/manual/en/function.proc-get-status.php