服务器Go程序意外停止自动重启

判断进程是否挂掉

ps -ef | grep ./blog |wc -l

如果输出为1,说明进程挂掉了
如果输出为2,说明进程正常运行

编辑脚本来检测和完成重启

vim restart.sh

逻辑代码,每分钟检测一次,判断进程是否运行,无则完成重启

#!/bin/bash
while :       #循环,为了让脚本一直运行监控
do
  COUNT=`ps -ef | grep ./blog |wc -l`
  if [ "$COUNT" -gt 1 ];
  then
    echo "server service is ok"
  else
    echo "server servicie not exist"
    nohup ./blog > server.log 2>&1 &
  fi
  sleep 60
done

给脚本权限

chmod +x ./restart.sh

开启脚本后台运行监控

nohup ./restart.sh > restart.log 2>&1 &

总结

常见的进程监控工具有 Supervisor、Upstart、systemd、nodejs的pm2 等

Supervisor-进程守护工具

原文地址:https://www.cnblogs.com/niuben/p/14673440.html