I'm experimenting with PHP's pcntl_* functions using the PHP cgi ... I've never written a daemon before, and there doesn't seem to be a lot of information out there about how to do this with the pcntl functions.
So, I've read what I can find on the subject as it deals with UNIX programming. The goal is a script that will run forever, checking a "job queue" ... If jobs are waiting, use pcntl_fork() to handle the jobs. To this end, I've come up with this pseudo-code ... Before going WAY off in this direction, I'd like to submit this for comments by those who've had more experience with this sort of thing. ------------------ // run forever if necessary set_time_limit(0); // detatch from the controlling terminal if (!posix_setsid()) { die('could not detach from terminal'); } // setup signal handlers pcntl_signal(SIGTERM, "sig_handler"); pcntl_signal(SIGHUP, "sig_handler"); // loop forever waiting on jobs while(1) { // check queue for pending jobs // (db lookup omitted, let's assume jobs are waiting) $jobs_waiting = true; if($jobs_waiting) { $pid = pcntl_fork(); if($pid == -1) { die('could not fork'); } else if ($pid) { // parent // call waitpid to reap children that // have already terminated do { $tpid = pcntl_waitpid(-1,$status,WNOHANG); if($tpid == -1) die('error occurred while waiting for child'); } while (!$tpid); } else { // child // perform task on jobs waiting // when job(s) complete, quit exit(); } } // wait two seconds before checking queue again sleep(2); unset($jobs_waiting); } function sig_handler($signo) { // blah blah } -------------------- I am open to suggestions or comments on this approach! Please let me know if you think this looks nuts. Thanks, Clay -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php