How to execute daemonize php script to run with upstart

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

I would like to run a PHP script as a daemon with upstart .

I installed the /etc/init/super-mailer.conf file as follows:

 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 starts once.

However, it does not start again. Why not?

I also tried replacing the string exec sudo 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 a loop? How can I say that you cannot run the script?

+6
source share
1 answer

A daemon is a type of program that does not stop until it is told. However, your script ends. So yes, you need to create a loop in a script that will run it every time.

However, keep in mind that re-executing a loop and executing your script again may require many processor cycles. So you can think of calling a function like usleep at each iteration to make deamon a bit less CPU consumption. So, for example, you can run a script every 2 seconds.

+5
source

All Articles