简体   繁体   English

如何使该服务器进程永久运行?

[英]How to make this server process run forever?

I have this Javascript Signal server running using nodejs. 我有使用nodejs运行的Javascript Signal服务器。

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. 我正在使用以下无限循环重新启动nodejs脚本(如果它已崩溃或未运行)。 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. 任何人都可以优化它,或者如果该进程突然停止运行,还有没有更好的方法来保持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

If you're using the new CentOS 6, a much better way to handle this is to put it in an Upstart script. 如果您使用的是新的CentOS 6,一种更好的处理方法是将其放入Upstart脚本中。 Upstart monitors all the system daemons and makes sure they stay running. Upstart监视所有系统守护程序,并确保它们保持运行状态。 The Upstart config below will also launch your process when the system boots. 系统启动时,下面的Upstart配置还将启动您的进程。

edit the file /etc/init/a.conf and put the following config in it. 编辑文件/etc/init/a.conf并将以下配置放入其中。 You'll need to sudo to edit as root. 您需要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

Now that you've created an Upstart config for your process you can start it from the command line: 现在,您已经为流程创建了Upstart配置,您可以从命令行启动它:

$ sudo service a start

Upstart will monitor your process and will restart it any time it goes down. Upstart将监视您的进程,并在发生故障时将其重新启动。 It also redirects logs to /var/log/upstart/a.log . 它还会将日志重定向到/var/log/upstart/a.log

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM