BASH - 如何使这个服务器进程永远运行? [英] BASH - how to make this server process run forever?

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

问题描述

我有这个Javascript信号服务器运行使用nodejs。

I have this Javascript Signal server running using nodejs.

但是每天它的崩溃,结果整个服务下降。我使用以下无限循环重新启动nodejs脚本,如果它崩溃或不运行。但它不完美的工作。

But daily its crashing as a result whole service goes down. I am using following infinite loop to restart the nodejs script if its crashed or not running. But its 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 并放入以下配置。

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆