从 PHP 调用外部 shell 脚本并获取其进程 ID

发布于 2024-08-06 03:40:48 字数 59 浏览 9 评论 0原文

如何从 PHP 本身调用外部 shell 脚本(或者外部 PHP 脚本)并在同一脚本中获取其进程 ID?

How can I invoke an external shell script (Or alternatively an external PHP script) from PHP itself and get its process ID within the same script?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

送君千里 2024-08-13 03:40:49

最终对我有用的是使用 pgrep 来获取在 PHP 中调用 exec() 后执行的命令(或进程名称)的 PI​​D。

exec($command);
$pid = exec("pgrep $command");

这也适用于启动后台进程。但是,您必须记住将程序的输出通过管道传输到 /dev/null,否则 PHP 将挂起。另外,在调用 pgrep 时,不能包含命令的管道部分:

$command = "bg_process -o someOption";
exec($command + " > /dev/null &"); //Separate the pipe and '&' from the command
$pid = exec("pgrep $command");

请注意,如果系统使用同一个命令启动了多个进程,它将返回与该命令匹配的所有进程的 PID。给 pgrep 的命令。如果只传入进程名称,它将返回具有该进程名称的所有 PID。

What ended up working for me is using pgrep to get the PID of the command (or process name) executed after calling exec() in PHP.

exec($command);
$pid = exec("pgrep $command");

This will work for launching background processes too. However, you must remember to pipe the program's output to /dev/null or else PHP will hang. Also, when calling pgrep you can't include the pipe portion of the command:

$command = "bg_process -o someOption";
exec($command + " > /dev/null &"); //Separate the pipe and '&' from the command
$pid = exec("pgrep $command");

Note that if the system has multiple processes launched with the same exact command, it will return the PIDs of all processes which match the command given to pgrep. If you only pass in a process name, it will return all PIDs with that process name.

多情出卖 2024-08-13 03:40:48
$command =  'yourcommand' . ' > /dev/null 2>&1 & echo $!; ';

$pid = exec($command, $output);

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

$pid = exec($command, $output);

var_dump($pid);
染墨丶若流云 2024-08-13 03:40:48

如果您想严格使用 PHP 提供的工具来执行此操作,而不是 Unix 特定的魔法,您可以使用 < a href="http://php.net/manual/en/function.proc-open.php" rel="noreferrer">proc_openproc_get_status,尽管需要将描述符规范传递到 proc_open 使用起来非常冗长:

<?php

$descriptorspec = [
    0 => ['pipe', 'r'],
    1 => ['pipe', 'w'],
    2 => ['pipe', 'w']
];
$proc = proc_open('yourcommand', $descriptorspec, $pipes);
$proc_details = proc_get_status($proc);
$pid = $proc_details['pid'];

echo $pid;

If you want to do this strictly using tools PHP gives you, rather than Unix-specific wizardry, you can do so with proc_open and proc_get_status, although the need to pass a descriptor spec into proc_open makes it unpleasantly verbose to use:

<?php

$descriptorspec = [
    0 => ['pipe', 'r'],
    1 => ['pipe', 'w'],
    2 => ['pipe', 'w']
];
$proc = proc_open('yourcommand', $descriptorspec, $pipes);
$proc_details = proc_get_status($proc);
$pid = $proc_details['pid'];

echo $pid;
梦醒灬来后我 2024-08-13 03:40:48

对于跨平台解决方案,请查看 symfony/process

use Symfony\Component\Process\Process;
$process = new Process('sleep 100');
$process->start();
var_dump($process->getPid());

使用 Composer 安装 symfony/process 后(composer require symfony/process),您可能需要使用 composer dump-autoload 更新自动加载信息并然后使用 require __DIR__ 要求自动加载。 '/vendor/autoload.php';

另请注意,您只能获取正在运行的进程的 PID。有关 API 详细信息,请参阅文档

For a cross-platform solution, check out symfony/process.

use Symfony\Component\Process\Process;
$process = new Process('sleep 100');
$process->start();
var_dump($process->getPid());

After you install symfony/process with composer (composer require symfony/process), you may need to update autoloading info with composer dump-autoload and then require the autoload with require __DIR__ . '/vendor/autoload.php';.

Notice also that you can get PID of a running process only. Refer to the documentation for API details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文