Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to daemonize a php script to be run with upstart

I have a PHP script that has been running as a cron job. The script uses the DB to see if it has anything to do, and to make sure its brethren are not already running.

I'd like to run the PHP script as a daemon with upstart.

I've set up my /etc/init/super-mailer.conf file as this:

description "super mailer"
author "Rob Nugen"

start on startup
stop on shutdown
respawn

exec sudo -u www-data php -f /var/www/super-mailer/scripts/mailer.php

I execute sudo start super-mailer and it runs once.

It doesn't run again, though. Why not?

I've also tried replacing the exec sudo line with

script
    sudo -u www-data php -f /var/www/clubberia-mailer/scripts/mailer.php
end script

Do I need to change my PHP script to loop? How do I tell upstart to keep starting the script?

like image 860
Thunder Rabbit Avatar asked Jan 08 '13 08:01

Thunder Rabbit


1 Answers

A daemon is a type of program that does not stop until told so. However, your script terminates itself. So yes, you need to make a loop in your script, that will re-run it every time.

However, keep in mind that just making a loop and executing your script again and again, might make it consume many CPU cycles. So, you might consider calling a function like usleep in every iteration to make the deamon a little less CPU-consuming. So, for example, you let your script run every 2 seconds.

like image 123
kokx Avatar answered Sep 28 '22 06:09

kokx