Jan 23, 2009
Daemons in PHP
A ‘daemon’ in Linux is program that runs continuously in the background without user intervention processing data.
For instance if we need to parse a log file, or process uploaded videos.
So we write a script and `fork` it separately as an individual process, this can be done in PHP using the Process Control Extensions. Getting a good grip on it, will take some studying. Though the time spent on it would be a valuable if you are in for a lot a heavy duty processing. The pcntl extensions are not available in PHP by default, you have to compile the CGI or CLI version of PHP with –enable-pcntl configuration option when compiling PHP to enable Process Control support.
An another way to run a script in background when pcntl functions are not available to you, would be by writing the script in a unconditional while loop. But make sure you have the logic to stop the process on failure within the loop.
while (1) {
//The complete process
if (condition_to_stop === true) {
break;
}
Then execute this from a control script. Start this a separate process redirecting all the out put to the void.
$processId = shell_exec("nohup /usr/bin/php process.php 1>/dev/null 2>&1 &");
Make sure you do not depend on any output printed in the screen by process.php, as in the above command we send all the output including the error’s into the /dev/null black hole.
This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.