What's the best way to keep a PHP script running as a daemon?

后端 未结 9 1124
小蘑菇
小蘑菇 2020-12-24 03:37

What is the best way to keep a PHP script running as a daemon, and what\'s the best way to check if needs restarting.

I have some scripts that need to run 24/7 and f

9条回答
  •  北海茫月
    2020-12-24 04:37

    Daemon is a linux process that runs in background; apache or mysql are daemons. In a linux environment, we can run a background program using cronjob, but it has some limitations, and in some scenarios it' s not a good idea. For example, using cronjob, we can't control if the previously run has finished yet. So often it's more convenient run a process as a daemon.

    // Daemonize
    $pid = pcntl_fork(); // parent gets the child PID and child gets 0
    if($pid){ // if pid is not 0
         // Only the parent will know the PID. Kids aren't self-aware
         // Parent says goodbye!
         print "Parent : " . getmypid() . " exiting\n";
         exit();
    }
    print "Child : " . getmypid() . "\n";
    

    The code above is taken from very good article about how to create a daemon in php. You can read this at link

提交回复
热议问题