Windows 机器上的 PHP;在后台启动进程

发布于 2024-08-18 06:41:58 字数 469 浏览 5 评论 0原文

我正在寻找最好的方法,或者任何真正在后台从 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 技术交流群。

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

发布评论

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

评论(5

别在捏我脸啦 2024-08-25 06:41:58

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

善良天后 2024-08-25 06:41:58

这个PHP 手册中的函数有帮助吗?

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");
}

Will this function from the PHP Manual help?

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");
}
妄司 2024-08-25 06:41:58

尝试在 Windows 2000 服务器上使用 PHP 5.2.8 实现相同的目标。

这些解决方案都不适合我。 PHP 一直在等待响应。

发现解决方案是:

$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

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 :

$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
当梦初醒 2024-08-25 06:41:58

来自 exec 的 php 手册:

如果程序使用此函数启动,为了使其继续在后台运行,程序的输出必须重定向到文件或另一个输出流。如果不这样做将导致 PHP 挂起,直到程序执行结束。

即,将输出通过管道传输到文件中,而 php 不会等待它:

exec('myprog > output.txt');

根据记忆,我相信您可以在 exec 系列命令前面添加一个控制字符(就像使用 @ 一样)这也可以防止执行暂停 - 但不记得它是什么。

编辑找到了!在 UNIX 上,用 & 执行的程序prepending 将在后台运行。抱歉,对你帮助不大。

From the php manual for 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.

ie pipe the output into a file and php won't wait for it:

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.

Edit Found it! On unix, programs executed with & prepended will run in the background. Sorry, doesn't help you much.

昔梦 2024-08-25 06:41:58

在我的 Windows 10 和 Windows Server 2012 计算机上,在 pclose/popen 中可靠工作的唯一解决方案是调用 powershell 的 Start-Process 命令,如下所示:

pclose(popen('powershell.exe "Start-Process foo.bat -WindowStyle Hidden"','r'));

或者如果您想提供参数并重定向输出,则更详细:

pclose(popen('powershell.exe "Start-Process foo.bat 
             -ArgumentList \'bar\',\'bat\' 
             -WindowStyle Hidden
             -RedirectStandardOutput \'.\\console.out\' 
             -RedirectStandardError \'.\\console.err\'"','r'));

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:

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