Windows 机器上的 PHP;在后台启动进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2067900/
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
PHP on a windows machine; Start process in background
提问by William W
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.
我正在寻找最好的方法,或者任何真正在后台从 php 启动进程的方法,这样我就可以稍后在脚本中终止它。
Right now, I'm using: shell_exec($Command); The problem with this is it waits for the program to close.
现在,我正在使用: shell_exec($Command); 问题在于它等待程序关闭。
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.
当我执行 shell 命令时,我想要一些与 nohup 具有相同效果的东西。这将允许我在后台运行该进程,以便稍后在脚本中将其关闭。我需要关闭它,因为此脚本将定期运行,并且在运行时无法打开程序。
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?
我想过生成一个 .bat 文件来在后台运行命令,但即便如此,我以后如何终止该进程?
The code I've seen for linux is:
我在 linux 上看到的代码是:
$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
编辑:原来我不需要终止进程
采纳答案by Alix Axel
Will this function from the PHP Manualhelp?
function runAsynchronously($path,$arguments) {
$WshShell = new COM("WScript.Shell");
$oShellLink = $WshShell->CreateShortcut("temp.lnk");
$oShellLink->TargetPath = $path;
$oShellLink->Arguments = $arguments;
$oShellLink->WorkingDirectory = dirname($path);
$oShellLink->WindowStyle = 1;
$oShellLink->Save();
$oExec = $WshShell->Run("temp.lnk", 7, false);
unset($WshShell,$oShellLink,$oExec);
unlink("temp.lnk");
}
回答by Jelmer
shell_exec('start /B "C:\Path\to\program.exe"');
shell_exec('start /B "C:\Path\to\program.exe"');
The /B
parameter is key here.
该/B
参数在这里是关键。
I can't seem to find where I found this anymore. But this works for me.
我似乎再也找不到在哪里找到这个了。但这对我有用。
回答by Rohit
Tried to achieve the same on a Windows 2000 server with PHP 5.2.8.
试图在带有 PHP 5.2.8 的 Windows 2000 服务器上实现相同的目标。
None of the solutions worked for me. PHP kept waiting for the response.
没有一个解决方案对我有用。PHP 一直在等待响应。
Found the solution to be :
发现解决方案是:
$cmd = "E:\PHP_folder_path\php.exe E:\some_folder_path\backgroundProcess.php";
pclose(popen("start /B ". $cmd, "a")); // mode = "a" since I had some logs to edit
回答by Mike Godin
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:
在我的 Windows 10 和 Windows Server 2012 机器上,在 pclose/popen 中可靠运行的唯一解决方案是调用 powershell 的 Start-Process 命令,如下所示:
pclose(popen('powershell.exe "Start-Process foo.bat -WindowStyle Hidden"','r'));
Or more verbosely if you want to supply arguments and redirect outputs:
或者更详细地说,如果你想提供参数和重定向输出:
pclose(popen('powershell.exe "Start-Process foo.bat
-ArgumentList \'bar\',\'bat\'
-WindowStyle Hidden
-RedirectStandardOutput \'.\console.out\'
-RedirectStandardError \'.\console.err\'"','r'));
回答by Adam Hopkinson
From the php manual for exec
:
来自 php 手册exec
:
If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.
如果一个程序用这个函数启动,为了让它在后台继续运行,程序的输出必须被重定向到一个文件或另一个输出流。如果不这样做,将导致 PHP 挂起,直到程序执行结束。
ie pipe the output into a file and php won't wait for it:
即将输出通过管道传输到文件中,php 不会等待它:
exec('myprog > output.txt');
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.
从记忆中,我相信您可以将一个控制字符(就像您使用 @ 一样)添加到exec
命令系列中,它也可以防止执行暂停 - 但不记得它是什么了。
EditFound it! On unix, programs executed with & prepended will run in the background. Sorry, doesn't help you much.
编辑找到了!在 unix 上,以 & 开头的程序将在后台运行。抱歉,对你帮助不大。