Windows 机器上的 PHP;在后台启动进程
我正在寻找最好的方法,或者任何真正在后台从 php 启动进程的方法,这样我就可以稍后在脚本中杀死它。
现在,我正在使用: shell_exec($Command); 这样做的问题是它等待程序关闭。
我想要在执行 shell 命令时与 nohup 具有相同效果的东西。这将允许我在后台运行该进程,以便稍后在脚本中可以将其关闭。我需要关闭它,因为该脚本将定期运行,并且运行时程序无法打开。
我想过生成一个 .bat 文件来在后台运行该命令,但即便如此,稍后如何终止该进程?
我见过的 Linux 代码是:
$PID = shell_exec("nohup $Command > /dev/null & echo $!");
// Later on to kill it
exec("kill -KILL $PID");
编辑:结果我不需要终止进程
I'm looking for the best, or any way really to start a process from php in the background so I can kill it later in the script.
Right now, I'm using: shell_exec($Command);
The problem with this is it waits for the program to close.
I want something that will have the same effect as nohup when I execute the shell command. This will allow me to run the process in the background, so that later in the script it can be closed. I need to close it because this script will run on a regular basis and the program can't be open when this runs.
I've thought of generating a .bat file to run the command in the background, but even then, how do I kill the process later?
The code I've seen for linux is:
$PID = shell_exec("nohup $Command > /dev/null & echo $!");
// Later on to kill it
exec("kill -KILL $PID");
EDIT: Turns out I don't need to kill the process
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
shell_exec('start /B "C:\Path\to\program.exe"');
/B
参数是这里的关键。我似乎再也找不到在哪里找到这个了。但这对我有用。
编辑:我在研究不同的东西时找到了我的来源 https://superuser.com/a/591084/281094
shell_exec('start /B "C:\Path\to\program.exe"');
The
/B
parameter is key here.I can't seem to find where I found this anymore. But this works for me.
edit: I found my source while researching something different https://superuser.com/a/591084/281094
这个PHP 手册中的函数有帮助吗?
Will this function from the PHP Manual help?
尝试在 Windows 2000 服务器上使用 PHP 5.2.8 实现相同的目标。
这些解决方案都不适合我。 PHP 一直在等待响应。
发现解决方案是:
Tried to achieve the same on a Windows 2000 server with PHP 5.2.8.
None of the solutions worked for me. PHP kept waiting for the response.
Found the solution to be :
来自
exec
的 php 手册:即,将输出通过管道传输到文件中,而 php 不会等待它:
根据记忆,我相信您可以在
exec
系列命令前面添加一个控制字符(就像使用 @ 一样)这也可以防止执行暂停 - 但不记得它是什么。编辑找到了!在 UNIX 上,用 & 执行的程序prepending 将在后台运行。抱歉,对你帮助不大。
From the php manual for
exec
:ie pipe the output into a file and php won't wait for it:
From memory, I believe there is a control character that you can prepend (like you do with @) to the
exec
family of commands that also prevents execution from pausing - can't remember what it is though.Edit Found it! On unix, programs executed with & prepended will run in the background. Sorry, doesn't help you much.
在我的 Windows 10 和 Windows Server 2012 计算机上,在 pclose/popen 中可靠工作的唯一解决方案是调用 powershell 的 Start-Process 命令,如下所示:
或者如果您想提供参数并重定向输出,则更详细:
On my Windows 10 and Windows Server 2012 machines, the only solution that worked reliably within pclose/popen was to invoke powershell's Start-Process command, as in:
Or more verbosely if you want to supply arguments and redirect outputs: