dongyin5516 2013-05-20 22:37
浏览 58

使用unix运行php脚本作为后台进程

I am trying to run the php script (below) named daemon.php as a back ground process using unix.

#!/usr/bin/php
<?php
$count = 0;
while(true){
    $count = $count + 1;
    file_put_contents('daemon1.log', $count, FILE_APPEND);
    sleep(1);  
}
?>

If I run it in the foreground using the command below

php daemon.php

The file deamon1.log starts getting written to. Also if I enter the command:

ps | grep php

I get the out put

10573 ttys000    0:00.20 php daemon.php

If I try to run it as a background process using the command

php daemon.php &

I get the console out put

[1] 10584

And the command

ps | grep php

Returns

10584 ttys000    0:00.02 php daemon.php

But nothing is written to deamon1.log. Can anyone tell me what I'm doing wrong?

  • 写回答

1条回答 默认 最新

  • donglin8467 2013-05-22 08:16
    关注

    Try a different approach.

    $file_handler = fopen( 'daemon1tempname.log', 'a+') or die ('Can not work with the File!');
    
    $count = 0;
    while(true){
        $count = $count + 1;
        fwrite($file_handler, "Test {$count}
    " );
        sleep(1);  
    }
    fclose($file_handler);
    

    Notice the different name of the file. Just make sure you work with a new file. If that works, test it with the file you want. If that does not work, then you know it's a permission issue.

    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建