node异常停止解决方案之一:shell脚本守护node进程!

   本来是参照一哥们的博客做的,但是这哥们的shell脚本有问题在我的服务器上。所以自己修改了脚本做个记录。以后参考

node 程序出异常就停止,很蛋疼。所以就写一个shell脚本检测node 运行的端口 如果 端口空 就重新启动node  原理就是如此

1.在node项目根目录下新建sh文件 start.sh

vim start.sh

2.我的start.sh脚本如下:

#!/bin/bash
echo "start zibowx_app"
while true  
do
# 获取5000端口的进程号
 pid=$(lsof -i:8080|awk '{print $2}')
 echo ${pid}
 # 如果进程号为空,重启服务
 if [ ! -n "$pid" ];then
   echo "restart..."
   nohup npm start > run.log 2 >run.log 2>&1  &

 fi
 sleep 5
done
~      

node异常停止解决方案之一:shell脚本守护node进程!_第1张图片

 

3.后台执行此shel脚本

nohup sh start.sh &

4.停止

ps -ef | grep start.sh

找出进程id  kill掉

第2步:

ps -ef | grep node

找到进程id   kill掉 

你可能感兴趣的:(node)