Linux 如何使用 PHP exec() 获取正在运行的 php 脚本列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7182558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:53:03  来源:igfitidea点击:

How to get list of running php scripts using PHP exec()?

phplinuxunixexec

提问by Aram Alipoor

I need to know and kill if there is any processes running a specified PHP script. Is that possible to get list of processes running sample.php using exec() and a php script.

我需要知道并杀死是否有任何进程运行指定的 PHP 脚本。是否可以使用 exec() 和 php 脚本获取运行 sample.php 的进程列表。

采纳答案by Marc B

exec("ps auxwww|grep sample.php|grep -v grep", $output);

This would only work, though, if PHP is running in CGI mode. If it's running as a SAPI type thing, you'll never see "sample.php" in the process list, just 'httpd'.

不过,这只有在 PHP 以 CGI 模式运行时才有效。如果它作为 SAPI 类型的东西运行,您将永远不会在进程列表中看到“sample.php”,只会看到“httpd”。

回答by genesis

There isn't. Because PHP is ran through apache/nginx. In case of command-line-access, proccess is named PHP, not actual name of your script.

没有。因为 PHP 是通过 apache/nginx 运行的。在命令行访问的情况下,proccess 被命名为 PHP,而不是脚本的实际名称。

回答by Daniel Pereira

It depends on lots of factors including OS, PHP version, etc., but you could try using signals to get a script to give you its name and then terminate if it matches. Or, have the script register its pid and then compare with running processes.

这取决于许多因素,包括操作系统、PHP 版本等,但您可以尝试使用信号来获取脚本以提供其名称,然后在匹配时终止。或者,让脚本注册其 pid,然后与正在运行的进程进行比较。

http://stuporglue.org/handling-signals-in-php/

http://stuporglue.org/handling-signals-in-php/

回答by josh

this helped me kill rogue processes via a url parameter. i figured i'd contribute to the discussion in case someone else is still poking around for answers.

这帮助我通过 url 参数杀死了流氓进程。我想我会为讨论做出贡献,以防其他人仍在寻找答案。

load yikes.php. identify the process id (it should be the first integer you come to in each index of the array). copy and paste it into the url as ?pid=XXXXX. and it's gone.

加载 yikes.php。标识进程 ID(它应该是您在数组的每个索引中找到的第一个整数)。将其复制并粘贴到 url 中作为 ?pid=XXXXX。它不见了。

//http://website.com/yikes.php?pid=668
$pid = $_GET['pid'];
exec("ps auxwww|grep name-of-file.php|grep -v grep", $output);
echo '<pre>';
print_r($output);
echo '</pre>';
//
exec("kill $pid");