2014-11-03 104 views
0

我已经使用nodejs运行此Javascript信号服务器。如何让这个服务器进程永远运行?

但是每天都会崩溃,导致整个服务停止。如果发生崩溃或未运行,我正在使用以下无限循环重新启动nodejs脚本。但它并不完美。

任何人都可以优化它,或者有没有更好的方法来保持a.js启动并运行,如果突然间进程没有活着。

#!/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 
+0

为什么/在哪里/如何首先崩溃? – mscdex 2014-11-03 01:55:49

+0

它与一些无效的变量未分配崩溃(一些代码错误,因为服务器脚本很大,我们想临时让它自动重启) – YumYumYum 2014-11-03 09:48:04

回答

2

如果您正在使用新的CentOS 6,一个更好的方法来处理,这是把它放在一个暴发户脚本。 Upstart监视所有系统守护进程并确保它们保持运行。当系统启动时,下面的Upstart配置也会启动你的进程。

编辑文件/etc/init/a.conf并在其中放入以下配置。你需要sudo以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 

现在你已经创建了你的过程是一个暴发户的配置,你可以在命令行中启动它:

$ sudo service a start 

新贵将监视您的程序,并将重新启动它的股价下跌的任何时间。它还将日志重定向到/var/log/upstart/a.log