如何从php执行shell命令而不等待WINDOWS上的输出

问题描述:

我有一个面具检测 python 程序.每当我们运行它时,它都会持续运行并不断检测(无休止的程序).我已经创建了一个门户网站来使用 php 在按钮单击事件上启动这个 python 程序.现在的问题是,当我通过 shell_exec() 启动这个程序时,它会启动.但是 php 等待程序完成.直到程序运行时(来自 shell_exec() ),php 只是冻结,甚至不加载任何其他页面.

I have a mask detection python program. Whenever we run that, it keeps running continuously and keeps detecting (endless program). I have made a web portal to start off this python program on a button click event using php. Now the issue is that when i start off this program by shell_exec() , it starts. But php waits for the program to finish. Till the time program is running (from shell_exec() ), php just freezes, doesn't even load any other page.

那么如何从 shell_exec() 运行 cmd 命令而不是等待它完成执行?

So how to run cmd command from shell_exec() and not wait for it to finish executing?

代码

$command = escapeshellcmd("start cmd /c python mask_detector.py " );
$output = shell_exec($command);

我在这个博客上找到了我的问题的简单解决方案 https://subinsb.com/how-to-execute-command-without-waiting-for-it-to-finish-in-php/

I found and easy solution for my question on this blog https://subinsb.com/how-to-execute-command-without-waiting-for-it-to-finish-in-php/

感谢这位天才

这里给出的解决方案是这个函数

Solution given here is this function

function <span style="color: red;">bgExec</span>($cmd) {
 if(substr(php_uname(), 0, 7) == "Windows"){
  pclose(popen("start /B ". $cmd, "r")); 
 }else {
  exec($cmd . " > /dev/null &"); 
 }
}

我尝试将我的标准输出重定向到 null,但它在 Windows 平台上对我不起作用.以上函数使用 popen() 和 pclose() 函数.这样我的工作就完成了.

I tried redirecting my stdout to null, but it didn't worked for me in Windows platform. Above function uses popen() and pclose() functions. That gets my job done.

pclose(popen("start cmd /c python demo.py", "r"));