簡體   English   中英

如何使該服務器進程永久運行?

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

我有使用nodejs運行的Javascript Signal服務器。

但是每天都在崩潰,結果整個服務崩潰了。 我正在使用以下無限循環重新啟動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

如果您使用的是新的CentOS 6,一種更好的處理方法是將其放入Upstart腳本中。 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

現在,您已經為流程創建了Upstart配置,您可以從命令行啟動它:

$ sudo service a start

Upstart將監視您的進程,並在發生故障時將其重新啟動。 它還會將日志重定向到/var/log/upstart/a.log

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM