In your node javascript insert the following near the beginning:
process.title="<yourapp>" fs.writeFileSync("/var/run/"+process.title+".pid", process.pid)
replace '<yourapp>' with the name of your application
Create at daemon script:
nano /etc/init.d/<yourapp>.sh
Insert the following text in the script file:
#!/bin/bash ### BEGIN INIT INFO # Provides: <yourapp> # Required-Start: $local_fs $syslog # Required-Stop: $local_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Start chat at boot time # Description : Start chat at boot time ### END INIT INFO # Start APP=<yourapp> SERVER_DIR=/server/$APP; SERVER_FILE=server.js PID_FILE=/var/run/$APP.pid NODE_DIR=/opt/node/bin PATH=$NODE_DIR:$SERVER_DIR:$PATH . /lib/lsb/init-functions do_start() { cd $SERVER_DIR log_action_begin_msg "Starting $APP" stop node $SERVER_FILE & log_action_end_msg $? } do_stop() { cd $SERVER_DIR log_action_begin_msg "Stopping $APP" stop log_action_end_msg $? } stop() { if [ -f $PID_FILE ]; then kill -9 `cat $PID_FILE` rm -f $PID_FILE fi } case $1 in start) do_start ;; stop) do_stop ;; restart) do_stop do_start ;; esac
Save and exit
Make application start at startup:
In terminal go to /etc/init.d and enter:
rc-update.d <yourapp>.sh defaults