I have this Javascript Signal server running using nodejs.
But daily it's crashing as a result whole service goes down. I am using following infinite loop to restart the nodejs script if it's crashed or not running. But it's not perfectly working.
Can anyone optimise it or is there any better way to keep the a.js up and running always if suddenly the process was not alive.
#!/bin/bash
while :
do
# 1
videoupload=$(pgrep -f "a.js")
if [ $videoupload ]; then
log1="running a $1 $2"
else
log1="re-launch a $1 $2"
nohup node /var/tmp/signal/a.js 2>&1 | tee -a /var/tmp/signal.log &
fi
echo $log1
sleep 1
done
If you're using the new CentOS 6, a much better way to handle this is to put it in an Upstart script. Upstart monitors all the system daemons and makes sure they stay running. The Upstart config below will also launch your process when the system boots.
edit the file /etc/init/a.conf
and put the following config in it. You'll need to sudo
to edit as root.
description "a.js"
author "YumYumYum"
# Stanzas
#
# Stanzas control when and how a process is started and stopped
# See a list of stanzas here: http://upstart.ubuntu.com/wiki/Stanzas#respawn
# When to start the service
start on runlevel [2345]
# When to stop the service
stop on runlevel [016]
# Automatically restart process if crashed
respawn
script
echo $$ > /var/run/a.pid;
exec node /var/tmp/signal/a.js
end script
post-stop script
rm -f /var/run/a.pid
end script
Now that you've created an Upstart config for your process you can start it from the command line:
$ sudo service a start
Upstart will monitor your process and will restart it any time it goes down. It also redirects logs to /var/log/upstart/a.log
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.