0

我有一个关于这个答案的问题 php 执行后台进程

谁能给我一个简单的例子来尝试答案解释?

提前致谢!

阿尔贝托

4

1 回答 1

2

仅在 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 */
于 2010-12-16T16:34:13.107 回答