Restart the Node.js application with init.d

I want the init.d daemon to restart my node.js application if it works. This script starts / stops my node application. I was not lucky that he restarted the application if it worked.

I work under CentOS. What am I missing?

#!/bin/sh . /etc/rc.d/init.d/functions USER="rmlxadmin" DAEMON="/usr/bin/nodejs" ROOT_DIR="/home/rmlxadmin" SERVER="$ROOT_DIR/my_node_app.js" LOG_FILE="$ROOT_DIR/app.js.log" LOCK_FILE="/var/lock/subsys/node-server" do_start() { if [ ! -f "$LOCK_FILE" ] ; then echo -n $"Starting $SERVER: " runuser -l "$USER" -c "$DAEMON $SERVER >> $LOG_FILE &" && echo_success || echo_failure RETVAL=$? echo [ $RETVAL -eq 0 ] && touch $LOCK_FILE else echo "$SERVER is locked." RETVAL=1 fi } do_stop() { echo -n $"Stopping $SERVER: " pid=`ps -aefw | grep "$DAEMON $SERVER" | grep -v " grep " | awk '{print $2}'` kill -9 $pid > /dev/null 2>&1 && echo_success || echo_failure RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f $LOCK_FILE } case "$1" in start) do_start ;; stop) do_stop ;; restart) do_stop do_start ;; *) echo "Usage: $0 {start|stop|restart}" RETVAL=1 esac exit $RETVAL 
+6
source share
1 answer

For this you need to use additional tools such as node-supervisor .

  • Install node -supervisor with npm:

    sudo npm install -g supervisor

  • Change the DAEMON variable in the init.d script file to node -supervisor: / usr / bin / supervisor. You can check this path using the "whereis supervisor" command on your system (after installation, of course).

Now the supervisor will restart your application if it works.

+8
source

Source: https://habr.com/ru/post/917488/


All Articles