May 20 2008

Making PHP program as Daemon

Category: PHPBipin Balakrishnan @ 10:05 pm

It is some time necessary to make a PHP progam as a daemon .Here it explains how it is possible. Process Control support in PHP implements the Unix style of process creation, program execution, signal handling and process termination. Process Control should not be enabled within a web server environment and unexpected results may happen if any Process Control functions are used within a web server environment.

The process control functions come along with the php-cli in linux.So we don’t need to install extra libraries for this to work.If php-cli is not there in your machine intall it with yum or apt-get.As i am using the ubuntu os im explaining with its command Ubuntu users can follow this.

bipin@bipin-laptop:~$ sudo apt-get install php5-cli

Copy and paste the code for background process

<?php
include_once('createdb.php');
declare(ticks=1);
$pid = pcntl_fork();
if ($pid == -1) {
die("could not fork");
} else if ($pid) {
exit(); // we are the parent
} else {
// we are the child
}
// detatch from the controlling terminal
if (posix_setsid() == -1) {
die("could not detach from terminal");
}
$posid=posix_getpid();
$fp = fopen("/var/run/process.pid", "w");
fwrite($fp, $posid);
fclose($fp);
// setup signal handlers
 pcntl_signal(SIGTERM, "sig_handler");
 pcntl_signal(SIGHUP, "sig_handler");
// loop forever performing tasks
 $dbobject = new DB();
 $dbobject->getCon();
 while (1) {
// do something interesting here, here i have called a function from other flile called "createdb.php"
$dbobject->CopyCallFiles();
}
 fclose($fp);
 function sig_handler($signo)
 {
switch ($signo) {
 case SIGTERM:
 // handle shutdown tasks
 exit;
 break;
 case SIGHUP:
 // handle restart tasks
 break;
 default:
 // handle all other signals
 }
}
?>

Run the program and the function $dbobject->CopyCallFiles(); will work as a deamon here we can include a file instead of the function also here.

Tags: , ,

15 Responses to “Making PHP program as Daemon”

  1. anon says:

    sonic server daemon or nanoserv?

  2. Sam says:

    Very handy, just what I was looking for!

  3. admin says:

    anon : Im not familiar with both.But this is a simplest way to create a daemon in PHP i think.Will appreciatable if you tell more about sonic and nano here.

  4. Computer Security & Solutions says:

    PHP is the way to go – Fantastic article, thanks for sharing this valuable resource.

  5. ldaves says:

    when i execute it
    eg. /usr/php5/bin/php mydaemon.php

    and logout. when i come back to check it out again it doesn’t run anymore. why is it lieke that?

  6. admin says:

    Hello ldaves,
    What you meant by logout here.Is it logging out from the system or from your application?.As its a daemon process which runs in back ground, it doesnt make any sense with log out.You will be wrong in the code, please read the post again and check the your code.

  7. ldaves says:

    i mean by log out the system.

    i execute the code and then close the terminal window.
    then i come back with ps -ef but see nothing.

  8. admin says:

    Hello ldaves,
    Did you put the program or the function in while loop as i mentioned in the post.If so it should work as it worked for me.As a sample program you just make a file creation program and put it as what i told and call it with include statement and try.
    Use “ps aux | grep php” to check the process.

  9. ldaves says:

    Hi,

    Yeah. I did put the while loop.

    But no sure why whenever I exit the terminal the script will be closing as well. So I have to use nohup to keep it running bg though I exit the terminal.

    Sorry but I work in Solaris platform, ‘ps -aux | grep php’ does not work.

    Thanks alot for replying.

  10. frankvw says:

    I’d just like you to know that you saved my @$$ with this one! I’m developing a PHP CLI application on Ubuntu Linux that has to run in the background, and due to some nasty issue with some Debian-based distro’s (having to do with libraries, apparently) running a PHP cli script in the background from the commmand line (as in “php script.php &) doesn’t work on Ubuntu. (It works fine on many other distro’s that I have tried, though.)

    As I’m developing this for a client and we’re been working on it for several months I was looking at a platform change this late in the project, which is enough to keep ten guys like me awake nights… because there doesn’t seem to be an easy fix for this issue on Ubuntu.

    Fortunately your daemon code runs like a charm. Thank you so much – you’ve saved me a lot of sleepless nights!

    // FvW

  11. admin says:

    hello frankvw
    Happy to hear from you….

  12. Teft says:

    Awesome code! Just what I was looking for to create a ‘publishing queue’ :)

  13. Maarten says:

    Hello ldaves,

    the reason your script closes is most likely because you use echo’s that write to the STDOUT of your current session. If you logout, that will cause fatal errors and the daemon to die.

    So remove all echo’s and/or redirect output to a logfile.

    Hope this is usefull info.

  14. Run PHP script as a Daemon » Van-Hout says:

    [...] I needed to run a PHP script in the background on my Ubuntu 8.04 server, but didn’t know how to do this. Googling around I found the following script to daemonize PHP scripts on bipinb.com: [...]

  15. Keith says:

    Thanks for this, worked perfectly.

Leave a Reply