linux下怎么监控某个程序,如果挂掉就自动启动

搬瓦工机场JMS

比如我要监控mqtt的服务mosquitto
如果这个服务挂了,就马上执行下面的命令
mosquitto -c /etc/mosquitto/mosquitto.conf -d -v

请大佬们指点一下,谢谢
安徽网友:最简单的 下面脚本放到定时任务

  1. #!/bin/sh
  2. source /etc/profile
  3. retDesc=`ps -ef | grep "mosquitto" | grep -v grep`
  4. retCode=$?
  5. if [ ${retCode} -ne 0 ];
  6.     then
  7.     echo "`date` restart" >> /root/down.log
  8.     nohup mosquitto -c /etc/mosquitto/mosquitto.conf -d -v &
  9. else
  10.     echo "server on"
  11. fi

复制代码

新疆网友:supervisord
湖北网友:百度linux进程守护,很好用,守护进程后,异常终止什么的,会自动给启动
内蒙古网友:一个计划任务,每一段时间查一下
ps -x|grep
看能不能取到进程信息
如果不行就执行命令
或者用systemctl进程守护
restart = on error
上海网友:
新建~/mosquitto.sh

  1. #!/usr/bin/env bash
  2. pgrep -f "mosquitto -c /etc/mosquitto/mosquitto.conf -d -v"
  3. if [[ $? -ne 0 ]]
  4. then
  5.   mosquitto -c /etc/mosquitto/mosquitto.conf -d -v
  6.   echo "mosquitto is stating"
  7. else
  8.   echo "mosquitto is running"
  9. fi

复制代码修改权限

  1. chmod +x ~/mosquitto.sh

复制代码添加crontab任务,例如每一分钟执行一次

  1. crontab -e
  2. */1 * * * * ~/mosquitto.sh  > /dev/null 2> /dev/null

复制代码
天津网友:monit 挺好用啊
台湾网友:

  1. while true; do if ! [[ `ps aux|grep mosquitto |grep -v grep` ]] ;then mosquitto -c /etc/mosquitto/mosquitto.conf -d -v; sleep 10;fi;done;

复制代码

山西网友:大佬,不行啊。。没有成功启动mqtt。。。
澳门网友:monit +1 进程挂了还可以发邮件通知
广东网友:monit +1 进程挂了还可以发邮件通知
江西网友:docker 运行 然后加个–restart=always

云南网友:不用放cron,终端执行不关闭窗口就行
湖南网友:
我的终端结果要关闭的。。

为啥放进cron就不行了呢?我写了日志,是成功执行的。。但是实际上并没有开。。
贵州网友:#!/bin/bash
port=`netstat -nltp | grep 3306 | wc -l`
if [ $port -ne 1 ]
then
systemctl start mysqld
echo "Starting MySQl.. SUCCESS!"
else
echo "MySQL is running."
fi

西藏网友:然后守护程序挂了
西藏网友:mark
内蒙古网友:除非你rm -rf /*
贵州网友:直接上supervisord啊
吉林网友:mark
西藏网友:mark
云南网友:没有任何提示,我看cron的日志也是正常执行的。直接执行脚本也是正常的。但是就是放进cron里面就不行了
安徽网友:
你的意思是直接运行脚本是正常的,
也就是mosquitto运行时输出mosquitto is running
mosquitto未运行的情况会启动mosquitto,并输出mosquitto is stating,
通过运行pgrep -f "mosquitto -c /etc/mosquitto/mosquitto.conf -d -v"查看结果不为0即确实启动了mosquitto

cron不会是用户权限的问题吧,cron添加到/etc/crontab,语句改为
*/1 * * * * root ~/mosquitto.sh  > /dev/null 2>&1
重庆网友:应该不是权限问题,我ssh连的就是root账号,而且我看cron的日志里面是有执行这个脚本文件的
重庆网友:crontab只添加mosquitto -c /etc/mosquitto/mosquitto.conf -d -v 这个呢
看能不能正常启动
广西网友:就是不执行脚本?直接执行这个命令?
上海网友:写个systemd,把restart写成always
辽宁网友:啥意思,大佬,咋弄?
湖北网友:嗯
也就是变量控制,来确定是哪部分的问题
北京网友:终于有一个懂得正道的了
四川网友:终于有一个懂得正道的了

未经允许不得转载:美国VPS_搬瓦工CN2 GIA VPS » linux下怎么监控某个程序,如果挂掉就自动启动

赞 (0) 打赏

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏