BASH Script to run node.js socket server as a service

Basically, what I want to execute is a kind of script or method for starting the node.js server socket script as a service.

This is done so that I do not need to physically run "node server.js" in SSH and are forced to sit there open.

Any help would be appreciated.

Thanks Scott

+4
source share
3 answers

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 
+4
source

It sounds like you need a tool like forever . There began a blog post here .

There are also more general tools like monit and upstart .

I personally use forever for my project and it works great.

+1
source

This quickly gets complicated. What you are really asking is to β€œdemonize” the process. This is more than just running in the background. Ideally, you want to run it in such a way that it does not link removable file systems, and you want to save the log output and not use it for your old terminal. You do not want to be accidentally killed when your shell came out, etc.

If you are on a Debian-based system such as Ubuntu, /sbin/start-stop-daemon does a lot of what you want. It was pointed out that the Ubuntu Upstart configuration was written for him, and this option too. On other distributions, such as Fedora, systemd provides what is usually considered a "gold-plated" version of this automatic demonization service. It can demonize and manage basically any program that is not explicitly interactive.

+1
source

All Articles