Create a config file for your phpdaemon at /etc/init/phpdaemon.conf
# Info
description "PHP Daemon"
author "Benedict E. Pranata"
# Events
start on startup
stop on shutdown
# Automatically respawn
# respawn the job up to 10 times within a 5 second period.
respawn
respawn limit 10 5
# Check before running
# exit if php script already running
pre-start script
if ps -ef | grep -v grep | grep phpdaemon; then
stop; exit 0
fi
end script
# Run the script!
# Note, in this example, if your PHP script returns
# the string "ERROR", the daemon will stop itself.
script
[ $(exec /usr/bin/php -f /path/to/your/phpdaemon.php) = 'ERROR' ] && ( stop; exit 1; )
end script
sudo service phpdaemon start
sudo service phpdaemon stop
sudo service myphpworker status