码迷,mamicode.com
首页 > 系统相关 > 详细

linux下监视进程挂掉后自动重启的shell脚本

时间:2015-06-15 11:26:52      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:

本文介绍的这个shell脚本,通过一个while-do循环,用ps -ef|grep 检查loader进程是否正在运行,如果没有运行,则启动,确保崩溃挂掉的进程,及时自动重启。

脚本内容如下:

#!/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

注意:
1、ps |grep 一个进程时必须加上路径,否则grep时会有不明错误;
2、必须用 -v 从结果中去除grep命令自身,否则结果非空。

如果启动此shell时发现进程已经存在,说明以别的方式启动了进程而不是此shell,那么它会持续提醒找到进程。
解决办法:
只用此shell启动服务,或一经发现以其他方式启动的服务即kill掉,即以上脚本中的这句来实现:
kill -9 $pidof $PWD/loader


linux下监视进程挂掉后自动重启的shell脚本

标签:

原文地址:http://my.oschina.net/u/1024573/blog/466776

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!