Creating Daemon in PHP using Upstart Centos

Its ultimately easy to create daemon in php since centos 6 introduce upstart. Let see..

Firstly, create the upstart configuration:

vim /etc/init/senderdaemon.conf

Add the following to the senderdaemon.conf

description “sender daemon tester”

author “Ogi Sigit P”

 

start on startup

stop on shutdown

respawn

 

script

exec php -f /path/to/php/file/senderdaemon.php

end script

Create the php file

vim /path/to/php/file/senderdaemon.php

Copy paste the below code to the php file:

#!/usr/bin/php

<?php

while(true){

   file_put_contents(‘/var/log/Daemon.log’, $i . ‘-Running…’. “\n”, FILE_APPEND);

   sleep(1);

$i++;

if( $i == 10 )

die();

}//end while

 

make sure the php is executable:

chmod +x /path/to/php/file/senderdaemon.php

Voila, its done! So easy isn’t it?

Now you may start your daemon using standart initctl command

initctl start senderdaemon

and also stop it

initctl stop senderdaemon

And you could make sure if your daemon is running

tail -f /var/lod/Daemon.log

One comment on “Creating Daemon in PHP using Upstart Centos

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>