php脚本获取管道的输入数据
cat get_pipedata.php
#!/usr/bin/env php <?php #======================================================== #testing how to get data from pipe #======================================================== $fp=fopen('php://stdin','r'); $result=''; while(!feof($fp)){ $result.=fgets($fp,128); } fclose($fp); echo $result;
新版:
#!/usr/bin/env php <?php $fp=fopen('php://stdin','r'); $content=''; if($fp){ while($line=fgets($fp,4096)){ echo $line.PHP_EOL; } fclose($fp); }
测试:
echo 'test message'|get_pipedata.php
tail -f -n +1 /var/log/user.log|get_pipedata.php