I do not see anything criminal wanting to use something other than forever . In my project, I also avoid using such tools and rely more on the capabilities of the system. Since I also try not to run my application as root, I cannot use SystemV and Upstart.
And then comes the mighty shell scripting ! I created several bash scripts that perform simple tasks, such as a process watchdog with the ability to start, stop, restart, and request status.
Check out this code. Feel free to change it like you do. To use it, enter the command in the COMMAND parameter. And do ./path_to_script.sh -start . This will create a watchdog process that will start your node process and see if it will die or not, and if so, it will restart it. This is far from ideal, so if someone has something to fix, add, delete here, do not hesitate to comment below.
#!/bin/bash CURRENT_PATH=$(pwd) LOGFOLDER=$CURRENT_PATH"/logs/" PIDFOLDER=$CURRENT_PATH"/pid/" #PID file where the this script process ID is stored WATCHDOGPIDFILE=$PIDFOLDER"watchdog-admin.pid" #PID file where the node process ID is stored NODEPIDFILE=$PIDFOLDER"node-admin.pid" #Watchdog process error log file WATCHDOGLOGFILE=$LOGFOLDER"admin-watchdog-error.log" #Node process error log file NODELOGFILE=$LOGFOLDER"admin-error.log" #Command to be executed on daemon start COMMAND="node ./admin/app.js 1> /dev/null 2>> $NODELOGFILE" ARG_1=$1 start() { if [ -e $NODEPIDFILE ]; then PID=$(cat $NODEPIDFILE) if [ $(ps -o pid | grep $PID) ]; then return; else touch $NODEPIDFILE nohup $COMMAND & echo $! > $NODEPIDFILE fi else touch $NODEPIDFILE nohup $COMMAND & echo $! > $NODEPIDFILE fi } stop() { if [ -e $NODEPIDFILE ]; then PID=$(cat $NODEPIDFILE) if [ $(ps -o pid | grep $PID) ]; then kill -9 $PID fi rm $NODEPIDFILE fi } stopdaemon() { stop rm $WATCHDOGPIDFILE exit 0 } log() { echo $1 >> $WATCHDOGLOGFILE } keep_alive() { if [ -e $NODEPIDFILE ]; then PID=$(cat $NODEPIDFILE) if [ $(ps -o pid | grep $PID) ]; then return; else log "Jim, he is dead!! Trying ressurection spell..." start fi else start fi } case x${ARG_1} in x-start ) echo "Starting daemon watchdog" nohup "$0" -daemon &> /dev/null & ;; x-daemon ) if [ -e $WATCHDOGPIDFILE ]; then PID=$(cat $WATCHDOGPIDFILE) if [ $(ps -o pid | grep $PID) ]; then exit 0; fi fi touch $WATCHDOGPIDFILE echo $$ > $WATCHDOGPIDFILE #trap the interruption or kill signal trap stopdaemon INT SIGINT TERM SIGTERM start while true; do keep_alive wait sleep 1 done ;; x-stop ) echo "Stopping daemon watchdog" PID=$(cat $WATCHDOGPIDFILE) kill $PID ;; x-status ) #check if process is running and PID file exists, and report it back ;; x ) echo "Usage {start|stop|status}" esac exit 0
source share