php
2016-08-16 125 views 1 likes 
1

我有一个脚本来下载一个torrent文件我使用的ctorrent似乎并没有关闭自己,所以我需要杀死pid。PHP获得进程的PID并将其杀死

$command = "ctorrent -x \"/var/www/html/torrents/$torrentName\""; 
$output = shell_exec($command); 

这完美的作品,我看到了另一个计算器问题,有人说要做到以下几点:

$command = 'yourcommand' . ' > /dev/null 2>&1 & echo $!; '; 
$pid = exec($command, $output); 
var_dump($pid); 

但是当我使用这一点,删除下载文件部分的输出,我需要作为即时通讯从输出中获取一些数据。

我怎样才能得到运行我的脚本的PID?

这最终是我想要实现:

if (file_exists("/proc/$pid")){ 
    shell_exec("kill -9 $pid"); 
} 
+1

您可以使用ps工具或使用锁定文件。对于ps'exec(“ps -ef | grep'$ process'”,$ output);'并解析$ output – ineersa

+0

'$ process'中将包含什么? – Exoon

+0

'ctorrent'我想? – ineersa

回答

0

这是我做的到底的方式,感谢大家的建议,我注意到当我做了什么ineersa说,并把. ' & echo $!; ';在最后它会添加PID在我的输出文本的开始,所以然后我刚刚捕获输出的第一行,它的工作原理大。

$command = "ctorrent -x \"/var/www/html/torrents/$torrentName\""; 
$output = shell_exec($command); 
echo "<pre>"; 
echo $output; 
echo "</pre>"; 

$pid = strtok($output, "\n"); 
echo $pid; 
0

刚刚尝试了pgrep命令。

$command = "pgrep programname"; 
$pid = shell_exec($command); 
+2

唯一的问题是如果我同时下载5个种子,我将如何知道哪个要杀死? – Exoon

0

我建议以下解决方案:

$command = 'ctorrent -x "/var/www/html/torrents/'.$torrentName.'"'; 
$outputfile = "output.out"; 
$pidfile = "pidfile.pid"; 
exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $command, $outputfile, $pidfile)); 
$pid = file_get_contents($pidfile); 
$outout = file_get_contents($outputfile); 

为你写你可以杀死进程:

if (file_exists("/proc/$pid")) { 
    shell_exec("kill -9 $pid"); 
} 
相关问题