简体   繁体   English

在后台从PHP执行.bat文件

[英]execute .bat file from php in background

I try to execute a .bat file (Windows Server with xampp) from php in Background. 我尝试在后台从php执行.bat文件(带有xampp的Windows Server)。 After i klick on the button, it also should go to another Website. 单击按钮后,它也应该转到另一个网站。 But when i click on the button, the browser is waiting for the script to finish. 但是,当我单击按钮时,浏览器正在等待脚本完成。 Mostly this is ending in a timeout. 通常,这以超时结束。

my php code: 我的php代码:

if (isset($_POST['test']))
{
    exec('C:\Daten\test.bat');
    header("Location:test_status.php");
}

how can i tell the php exec, to dont wait? 我怎样才能告诉php exec,不要等待?

I tried also following, but does not work: 我也尝试了以下操作,但不起作用:

exec('C:\Daten\test.bat' . '> /dev/null &');

Thanks for the response, but this did not work for me. 感谢您的回复,但这对我不起作用。 I found that many solution ideas in the internet, but nothing worked for me. 我在互联网上发现了许多解决方案的想法,但对我没有任何帮助。

Now i finally found the solution! 现在,我终于找到了解决方案! The keyword is popen! 关键字是popen! This works nice: 这很好用:

$handle = popen ('start /B C:\Data\restart_TS3.bat > C:\Daten\restart_TS3.log 2>&1', 'r');
pclose ($handle);
header("Location:ts3_status.php");

This do following: popen opens a background process that starts the batch file, output goes to the log file. 请执行以下操作:popen打开一个后台进程,该进程启动批处理文件,输出进入日志文件。 Then you need to close it with pclose. 然后,您需要使用pclose关闭它。 (the batch is still running in background) After that, it opens another website, in my case its a "see current status" website. (该批处理仍在后台运行)之后,它将打开另一个网站,在我的情况下,它是一个“查看当前状态”网站。

If you dont want a log, you can also output (like MarkB told before) with >nul. 如果您不想要日志,也可以使用> nul输出(如MarkB所述)。 This would look like that: 看起来像这样:

$handle = popen ('start /B C:\Data\restart_TS3.bat >nul 2>&1', 'r');

And be careful with empty spaces in the path! 并注意路径中的空白处! I dont know why, but this will not work:'start /B "C:\\Data Folder\\restart_TS3.bat" >nul 2>&1' In that cases you need " in the folder like that: 我不知道为什么,但是这行不通:'start / B“ C:\\ Data Folder \\ restart_TS3.bat”> nul 2>&1'在这种情况下,您需要在这样的文件夹中:

$handle = popen ('start /B C:\"Data Folder"\restart_TS3.bat >nul 2>&1', 'r');

This works fine with empty space in Folder names. 文件夹名称中有空白时,这可以正常工作。

You're using Windows. 您正在使用Windows。 there's no /dev/null in Windows (it's just nul ), and there's no & to run jobs in the background. Windows中没有/dev/null (只是nul ),并且没有&在后台运行作业。 & in cmd.exe is a command separator. &在cmd.exe中是命令分隔符。 So your exec() will hang/wait for the .bat to finish. 因此,您的exec()将挂起/等待.bat完成。

Try 尝试

exec('start c:\daten\test.bat');

instead, which would start the batch file as a separate process. 而是将批处理文件作为一个单独的过程启动。

This worked for me. 这对我有用。

exec("start cmd /c test.bat", $output);
var_dump($output);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM