我有一个关于这个答案的问题 php 执行后台进程
谁能给我一个简单的例子来尝试答案解释?
提前致谢!
阿尔贝托
仅在 linux 机器上:
结果.php
<?php
$cmd = 'for i in {1..5}; do date; sleep 2; done';
$outputfile = '/tmp/result.txt';
$pidfile = '/tmp/result.pid';
exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile));
?>
在命令行中:
php result.php;
tail /tmp/result.txt; /* to monitor the results, take 10 seconds to complete */
cat /tmp/result.pid /* the pid registered */