I need to run a php script as daemon process (wait for instructions and do stuff). cron job will not do it for me because actions need to be taken as soon as instruction arrives. I know PHP is not really the best option for daemon processes due to memory management issues, but due to various reasons I have to use PHP in this case. I came across a tool by libslack called Daemon (http://libslack.org/daemon) it seems to help me manage daemon processes, but there hasn't been any updates in the last 5 years, so I wonder if you know some other alternatives suitable for my case. Any information will be really appreciated.
You could start your php script from the command line (i.e. bash) by using
nohup php myscript.php &
& puts your process in the background.
Yes, there are some drawbacks, but not possible to control? That's just wrong.
kill processid will stop it. And it's still the best and simplest solution.
Another option is to use Upstart. It was originally developed for Ubuntu (and comes packaged with it by default), but is intended to be suitable for all Linux distros.
Create a new script file at
/etc/init/myphpworker.conf. Here is an example:
# Info description "My PHP Worker" author "Jonathan" # Events start on startup stop on shutdown # Automatically respawn respawn respawn limit 20 5 # 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/script.php) = 'ERROR' ] && ( stop; exit 1; ) end script
sudo service myphpworker start sudo service myphpworker stop
sudo service myphpworker status
A big thanks to Kevin van Zonneveld, where I learned this technique from.