Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php进程背景_Php_Process_Background Process - Fatal编程技术网

php进程背景

php进程背景,php,process,background-process,Php,Process,Background Process,关于这个答案我有个问题 有人能给我一个简单的例子来解释答案吗 提前谢谢 Alberto仅限linux机箱: result.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

关于这个答案我有个问题

有人能给我一个简单的例子来解释答案吗

提前谢谢

Alberto

仅限linux机箱:

result.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));
?>

这个答案在除shell会话之外的任何上下文中都是错误的,并且失败是一种可能的结果——生成的进程没有与会话分离,它只是并行运行,直到会话结束。您确定这适用于您的场景吗?
php result.php;
tail /tmp/result.txt; /* to monitor the results, take 10 seconds to complete */
cat /tmp/result.pid   /* the pid registered */