2024-07-28 05:00:06

PHP执行后台进程

我需要在用户操作时执行目录复制,但目录相当大,因此我希望能够在用户不知道完成复制所需时间的情况下执行这样的操作。

任何建议都将不胜感激。


当前回答

与其启动后台进程,不如创建一个触发器文件,并让像cron或autosys这样的调度程序定期执行一个寻找触发器文件并对其进行操作的脚本?触发器可以包含指令甚至原始命令(更好的方法是将其变成shell脚本)。

其他回答

如果你只需要在后台做一些事情,而不需要PHP页面等待它完成,你可以使用另一个(后台)PHP脚本,用wget命令“调用”。当然,与系统上的任何其他PHP脚本一样,这个后台PHP脚本将以特权执行。

下面是一个在Windows上使用gnuwin32包中的wget的例子。

后台代码(文件test-proc-bg.php)为例…

sleep(5);   // some delay
file_put_contents('test.txt', date('Y-m-d/H:i:s.u')); // writes time in a file

前台脚本,调用…

$proc_command = "wget.exe http://localhost/test-proc-bg.php -q -O - -b";
$proc = popen($proc_command, "r");
pclose($proc);

你必须使用popen/pclose才能正常工作。

wget选项:

-q    keeps wget quiet.
-O -  outputs to stdout.
-b    works on background

使用此函数在后台运行程序。它是跨平台的,完全可定制的。

<?php
function startBackgroundProcess(
    $command,
    $stdin = null,
    $redirectStdout = null,
    $redirectStderr = null,
    $cwd = null,
    $env = null,
    $other_options = null
) {
    $descriptorspec = array(
        1 => is_string($redirectStdout) ? array('file', $redirectStdout, 'w') : array('pipe', 'w'),
        2 => is_string($redirectStderr) ? array('file', $redirectStderr, 'w') : array('pipe', 'w'),
    );
    if (is_string($stdin)) {
        $descriptorspec[0] = array('pipe', 'r');
    }
    $proc = proc_open($command, $descriptorspec, $pipes, $cwd, $env, $other_options);
    if (!is_resource($proc)) {
        throw new \Exception("Failed to start background process by command: $command");
    }
    if (is_string($stdin)) {
        fwrite($pipes[0], $stdin);
        fclose($pipes[0]);
    }
    if (!is_string($redirectStdout)) {
        fclose($pipes[1]);
    }
    if (!is_string($redirectStderr)) {
        fclose($pipes[2]);
    }
    return $proc;
}

注意,在命令启动后,默认情况下,该函数关闭正在运行进程的stdin和stdout。你可以通过$redirectStdout和$redirectStderr参数将进程输出重定向到某个文件中。

windows用户注意: 不能以以下方式将stdout/stderr重定向为null:

startBackgroundProcess('ping yandex.com', null, 'nul', 'nul');

然而,你可以这样做:

startBackgroundProcess('ping yandex.com >nul 2>&1');

*nix用户注意事项:

1)如果你想得到实际的PID,使用exec shell命令:

$proc = startBackgroundProcess('exec ping yandex.com -c 15', null, '/dev/null', '/dev/null');
print_r(proc_get_status($proc));

2)使用$stdin参数,如果你想传递一些数据到你的程序的输入:

startBackgroundProcess('cat > input.txt', "Hello world!\n");

您可能想尝试将此附加到您的命令中

>/dev/null 2>/dev/null &

eg.

shell_exec('service named reload >/dev/null 2>/dev/null &');

如果希望通过PHP执行后台进程,请将命令的输出输送到/dev/null,并在命令末尾添加&。

exec("bg_process > /dev/null &");

注意,不能使用exec()的$output参数,否则PHP将挂起(可能直到进程完成)。

我知道这是一个有100年历史的帖子,但不管怎样,我觉得它可能对某人有用。你可以在页面的某个地方放一个不可见的图像,指向需要在后台运行的url,就像这样:

<img src="run-in-background.php" border="0" alt="" width="1" height="1" />