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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With