在后台windows中运行php脚本


run php script in the background - windows

我想运行php脚本,该脚本在另一个php页面的后台运行无限循环,而不会停止或影响调用方页面,到目前为止,我尝试的是:在呼叫方页面中:

set_time_limit(0);
ignore_user_abort(true); 
$path=realpath('./infinity.php');
$cmd='c:/xampp/php/php'.$path;
if (substr(php_uname(), 0, 7) == "Windows"){ 
   exec("start/b C:''xampp''php''php $path > NUL");
} 
else { 
    exec($cmd . " > /dev/null &");   
} 

在infinity.php 中

set_time_limit(0);
ignore_user_abort(true); 

我的问题是,当我试图在浏览器中打开调用方页面时,我会永远看到加载标记,而页面永远不会打开!

你能告诉我我缺了什么吗?注意:-我正在开发windows8-我在这两个页面中都使用了settimelimit(0)和ignoreuserabort(true),因为我不知道该如何使用它们。

我知道我的问题可能重复,但我已经尝试了所有建议的解决方案,但我仍然无法打开呼叫方页面

任何帮助都将不胜感激

这对我很有效,但请注意,这是在Linux盒子上!

$cmd = "/path/to/script/to/run/in/background.php $var1 $var2";
$outputfile = '/path/to/script/output.txt';
$pidfile = '/path/to/process/id/file.txt';
$exe = exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile));

这将在第一个脚本之外的shell中运行background.php。第一个脚本将继续正常运行,而不等待background.php完成。因为脚本将在后台运行,所以获取PID并将其存储在某个位置非常重要,这样您就可以不时地检查脚本。即它还在运行吗。outputfile和pidfile需要可由脚本写入。

很抱歉,在我意识到你在windows环境中工作之前,我就开始写这个回复了!也许仍然有一些用处。(可能不是..)