通过 shell_exec() 在后台执行命令
Executing command in background by shell_exec()
在 Linux 我可以使用 shell_exec()
如下 运行 background 中的命令:
shell_exec("/usr/bin/nohup curl -o /dev/null --data \"$data\" $url >/dev/null 2>&1 &");
请注意,上面的行不会等待结果,代码会立即恢复。
如何在 windows 上实现?最好是 windows.
的内置内容
我会检查这个用例的 Symfony Process 组件。它提供了同步和异步执行的方法。它可以通过 Composer 轻松安装,但至少需要 PHP 7.1。
可以在此处找到文档:https://symfony.com/doc/current/components/process.html
您的用例示例可能如下所示:
$process = new Process(['curl', '--data', $data, $url]);
$process->start();
请注意,我省略了 -o
选项,因为除非您明确请求,否则不会显示子流程的输出。
快速浏览发现一些帖子说 start()
方法在 Windows 上阻塞,但似乎已经实施并合并了一个补丁:https://github.com/symfony/symfony/pull/10420
编辑: 如果你不想使用库,你可以结合使用 popen()
、pclose()
和 Windows 工具 start
:
pclose(popen('start /B curl --data "'.$data.'" '.$url, 'r'));
这会在后台运行程序而不打开 window 并立即 returns。
编辑 2: start /B
技巧来源:http://php.net/manual/en/function.exec.php#86329
在 Linux 我可以使用 shell_exec()
如下 运行 background 中的命令:
shell_exec("/usr/bin/nohup curl -o /dev/null --data \"$data\" $url >/dev/null 2>&1 &");
请注意,上面的行不会等待结果,代码会立即恢复。
如何在 windows 上实现?最好是 windows.
的内置内容我会检查这个用例的 Symfony Process 组件。它提供了同步和异步执行的方法。它可以通过 Composer 轻松安装,但至少需要 PHP 7.1。
可以在此处找到文档:https://symfony.com/doc/current/components/process.html
您的用例示例可能如下所示:
$process = new Process(['curl', '--data', $data, $url]);
$process->start();
请注意,我省略了 -o
选项,因为除非您明确请求,否则不会显示子流程的输出。
快速浏览发现一些帖子说 start()
方法在 Windows 上阻塞,但似乎已经实施并合并了一个补丁:https://github.com/symfony/symfony/pull/10420
编辑: 如果你不想使用库,你可以结合使用 popen()
、pclose()
和 Windows 工具 start
:
pclose(popen('start /B curl --data "'.$data.'" '.$url, 'r'));
这会在后台运行程序而不打开 window 并立即 returns。
编辑 2: start /B
技巧来源:http://php.net/manual/en/function.exec.php#86329