I'd like to have an init.d daemon restart my node.js app if it crashes. This script starts/stops my node app. I've had no luck getting it to restart the app if it crashes.
我想让一个init.d守护进程重启我的node.js应用程序,如果它崩溃了。此脚本启动/停止我的节点应用程序。如果崩溃,我没有运气重启应用程序。
I'm running under CentOS. What am I missing?
我在CentOS下运行。我错过了什么?
#!/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
1 个解决方案
#1
8
You need to use additional tools like node-supervisor for this case.
在这种情况下,您需要使用其他工具,如node-supervisor。
-
Install node-supervisor with npm:
使用npm安装node-supervisor:
sudo npm install -g supervisor
sudo npm install -g supervisor
-
Change DAEMON variable in your init.d script to node-supervisor executable: /usr/bin/supervisor. You can check this path using command 'whereis supervisor' in your system (after installation, of course).
将init.d脚本中的DAEMON变量更改为node-supervisor可执行文件:/ usr / bin / supervisor。您可以使用系统中的“whereis supervisor”命令检查此路径(当然,在安装后)。
Now supervisor will restart your application if it's crash.
现在,如果应用程序崩溃,主管将重启您的应用程序。
#1
8
You need to use additional tools like node-supervisor for this case.
在这种情况下,您需要使用其他工具,如node-supervisor。
-
Install node-supervisor with npm:
使用npm安装node-supervisor:
sudo npm install -g supervisor
sudo npm install -g supervisor
-
Change DAEMON variable in your init.d script to node-supervisor executable: /usr/bin/supervisor. You can check this path using command 'whereis supervisor' in your system (after installation, of course).
将init.d脚本中的DAEMON变量更改为node-supervisor可执行文件:/ usr / bin / supervisor。您可以使用系统中的“whereis supervisor”命令检查此路径(当然,在安装后)。
Now supervisor will restart your application if it's crash.
现在,如果应用程序崩溃,主管将重启您的应用程序。