douyuan6490 2011-07-06 10:08
浏览 86

shell_exec - 记录和输出

I am running multiple shell_exec, process.php's run in the background

On the shell/ssh, I execute the code like this: username [~/public_html/curl]# php index.php

Example....

index.php

<?php
 shell_exec("php process.php > /dev/null 2>&1 &");
 shell_exec("php process.php > /dev/null 2>&1 &");
 shell_exec("php process.php > /dev/null 2>&1 &");
 shell_exec("php process.php > /dev/null 2>&1 &");
?>

process.php

<?php
$section = rand(999,999999);
$z = 1;
print "STARTED .... 
";
while($z <= 10) {
 print "---------------------------------
";
 print $section . ": " . $z . "
";
 $z++;
 sleep(2);
}
print "LOOP FINISH at " . time();
?>

when process.php's is running, I am having two problems with this:

  1. I cant not see the output from process.php's (I need to know what they are doing)
  2. I need to know which process is finish and which have started.

What the best way logging the output in real time? saving into text file? or how can it be done to mysql database (logs table)?

  • 写回答

1条回答 默认 最新

  • dqg63264 2011-07-06 10:15
    关注

    If your processes will be alive only within the lifetime of another php script, you could use popen instead of shell_exec:

    http://us.php.net/popen

    This gives you a very convenient way to get data from the other processes into your php script, using the same interface as file handles. To know then the process is done, you could make sure the process sends an EOF (end-of-file) when it's done, and use the feof php function to detect it.

    On the other hand, if your processes may live longer than any other php scripts that talk to it, then a text file may be a very practical solution. Keep in mind, though, that disk access is always much much slower than memory access, so if you use text files for communication, it will not be optimally fast.

    评论

报告相同问题?

悬赏问题

  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺