使用Shell或其他脚本语言编写一个针对Linux进程监控及故障重启的脚本

1.shell通过一个while-do循环,用ps -ef|grep检查loader进程是否正在运行,如果没有运行,则启动,

这样就保证崩溃挂掉的进程重新被及时启动。

注意2点:1.ps -ef |grep 一个进程时必须加上其路径,否则容易grep到错误的结果;

             2.必须用-v 从结果中去除grep命令自身,否则结果非空。

#!/bin/sh 

while
do 
echo "Current DIR is " $PWD 

stillRunning=$(ps -ef |grep "$PWD/loader" |grep -v "grep") 


if [ "$stillRunning" ] ; then 
echo "TWS service was already started by another way" 


echo "Kill it and then startup by this shell, other wise this shell will loop out this message annoyingly" 


kill -9 $pidof $PWD/loader 
else 


echo "TWS service was not started" 


echo "Starting service ..." 
$PWD/loader 


echo "TWS service was exited!" 


fi 
sleep 10 


done 

    如果启动此shell时发现进程以及存在,说明已别的方式启动了进程,那么他会持续提醒找到进程,

解决办法是,只要一经发现是其他方式启动此进程即kill掉(kill -9 $PWD/loader)

 

转载于:https://www.cnblogs.com/arvin-feng/p/11108750.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值