LAMP On Focus

Icon

PHP, MySQL, Linux practical usage.

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.

Calendar

March 2010
M T W T F S S
« Feb    
1234567
891011121314
15161718192021
22232425262728
293031  

Bookmarks

  • No bookmarks avaliable.

Friend Connect