本文介绍了如何使该服务器进程永久运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用nodejs运行的Javascript Signal服务器。

I have this Javascript Signal server running using nodejs.

但是每天都会崩溃,结果整个服务崩溃了。我正在使用以下无限循环重新启动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.

任何人都可以对其进行优化,或者如果突然之间该进程不存在,还有没有更好的方法可以使a.js始终保持运行。 / p>

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


推荐答案

如果您使用的是新的CentOS 6,一种更好的处理方法是将其放入Upstart脚本中。 Upstart监视所有系统守护程序,并确保它们保持运行状态。当系统启动时,下面的Upstart配置也会启动您的进程。

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.

编辑文件 /etc/init/a.conf 并将以下配置放入其中。您需要 sudo 才能以root用户身份进行编辑。

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

现在您已经为流程创建了Upstart配置您可以从命令行启动它:

Now that you've created an Upstart config for your process you can start it from the command line:

$ sudo service a start

Upstart将监视您的进程,并在发生故障时重新启动它。它还会将日志重定向到 /var/log/upstart/a.log

Upstart will monitor your process and will restart it any time it goes down. It also redirects logs to /var/log/upstart/a.log.

这篇关于如何使该服务器进程永久运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 03:16