WebSocket - Daemon with Upstart - Ubuntu / CentOS


With Ubuntu you should have upstart installed by default and therefore the daemon can be installed even easier.

Create a new script file at: /etc/init/jakchat.conf with following content:

# Info
description "Live Chat WebSocket"
author      "JAKWEB / Live Chat WebSocket"

# 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_THE_SOCKET_SERVER/server/socketserver.php) = 'ERROR' ] && ( stop; exit 1; )
end script

That will do the same and starts automatically on startup.

You can also manually start / stop and check the status with following lines:

sudo service jakchat start
sudo service jakchat stop
sudo service jakchat status

Much better solution than a simple bash script, but sadly not all linux server have upstart integrated by standard.