你的位置:首页 > 互联网IT

linux检测程序没有在线启动,程序没有运行,自动启动,检测程序,检测到进程被结束,马上自动启动;检测进程A如果关闭就自动运行指定程序运行,linux查看某进程是否运行,Linux检查指定程序是否在运行监控脚本

系统:linux

功能:检测程序没有在线自动启动


Windows检测到进程被结束,马上自动启动

https://www.zhuguodong.com/?id=430


说明


此脚本可以检测Linux指定名字的程序是否在运行,如果检测到没有在运行,则启动该程序并做日志记录


编写脚本


vi monitor.sh


将以下代码中的程序名和所在目录修改然后复制粘贴保存


#!/bin/sh


# 在这修改程序名和程序所在目录,其他不用改


name="entwallet"

path="/root/entwallet"

pid=`ps -A |grep $name| awk '{print $1}'`

now=`date "+%Y-%m-%d %H:%M:%S"`


# 检测是否在运行


if [ ! $pid ]

then

echo "$now $name is not running, start it now..."


# 启动程序命令


cd $path

./entwallet start

new_pid=`ps -A |grep $name| awk '{print $1}'`


# 检测是否启动成功


if [ ! $pid ]

then

echo "$now $name start successfully, pid is $new_pid"

else

"$now $name start failed!"

fi


else

echo "$now $name is running, pid is $pid"

fi



添加执行权限


chmod +x ./monitor.sh


添加定时任务


crontab -e


根据自己的需要编辑脚本运行时间,修改日志存放的目录,例如:1分钟检测一次


* * * * * /home/leafserver/monitor.sh >> /home/leafserver/monitor.log 2>&1


输入完成保存即可


查看日志


tail -f /home/leafserver/monitor.log


日志如下


2020-08-11 14:51:22 entwallet is not running, start it now...


2020-08-11 14:51:22 entwallet start successfully, pid is 17117


2020-08-11 14:52:01 entwallet is running, pid is 17117


2020-08-11 14:53:01 entwallet is running, pid is 17117


2020-08-11 14:54:01 entwallet is running, pid is 17117


2020-08-11 14:55:01 entwallet is running, pid is 17117


QQ截图20220405004917.jpg linux检测程序没有在线启动,程序没有运行,自动启动,检测程序,检测到进程被结束,马上自动启动;检测进程A如果关闭就自动运行指定程序运行,linux查看某进程是否运行,Linux检查指定程序是否在运 互联网IT


参考文章:

https://blog.csdn.net/weixin_35642587/article/details/116553915


  • 发表评论
  • 查看评论
【暂无评论!】

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。