how to daemonize a php script to be run with upstart
Asked Answered
T

1

6

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?

Telugu answered 8/1, 2013 at 8:52 Comment(1)
Did you implement daemonizing in your script? If not, it's going to run only once.Scofflaw
I
5

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.

Indiaindiaman answered 8/1, 2013 at 9:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.