如何在后台使用EXEC()和CMD执行PHP脚本 [英] How to execue PHP scripts in the background using EXEC() and CMD
问题描述
我不能使用 $ com = new Com('WScript.shell');
方式,它会导致我的系统出现问题。可能我需要升级我的PHP,因为它很老(5.2),但我想我的尝试是不依赖于版本。
I can't use the $com = new Com('WScript.shell');
way, it's causing problems on my system. Probably I need to upgrade my PHP because it's very old (5.2), but I suppose what I'm trying to do is not version dependent.
我正在运行PHP在Windows 2008 64位上。我可以运行一个以开头
开头的命令,在单独的控制台上运行该命令。
I'm running PHP on Windows 2008 64-bit. Where I can run a command prefixed with start
to run the command on a separate console.
start php myscript.php
如果从命令行执行,
我需要使用 exec only 。
exec("CMD /C start php myscript.php");
/ C
是终止CMD会话
但是它没有给出相同的结果,它仍然等待脚本终止!
But it's not giving the same results, it still waits for the script to terminate !
推荐答案
这个工作:
exec("start /B php myscript.php");
同时在myscript.php开头关闭所有标准流:
Also close all standard streams at the very beginning of myscript.php:
fclose(STDIN);
fclose(STDOUT);
fclose(STDERR);
如果您希望 echo
您可以在关闭标准流之后打开一个文件以写入stdout:
If you want echo
to still work, you can open a file for writing to stdout after closing the standard streams:
$STDIN = fopen('/dev/null', 'r');
$STDOUT = fopen('myscript.log', 'wb');
这篇关于如何在后台使用EXEC()和CMD执行PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!