用shell脚本监控进程是否存在 不存在则启动的实例,先上代码干货:
#!/bin/sh
ps -fe|grep processString |grep -v grep
if [ $? -ne 0 ]
then
echo "start process....."
else
echo "runing....."
fi
shell基本命令
(1)ps aux 显示系统全部进程,一行一个
(2)grep “abc” 从标准输入读取字符流,输出包含字符串“abc”的行
(3)grep -v "acb" 从标准输入读取字符流,输出不包含字符串“abc”的行
(4)wc -l 从标准输入读取字符流,输出行数
例如需要检测进程httpd是否存在,操作流程如下:
(1)读取系统所有进程
(2)判断包含指定进程名字的信息是否存在
通过管道连接,命令如下:
ps axu | grep "httpd" | grep -v "grep" | wc -l
所有进程-->获取包含“httpd”的行-->删除grep进程信息-->输出最后的行数
通过判断命令的执行结果是否为 0 ,可以知道进程是否存在。
脚本如下:
#!/bin/sh
while true;do
count=`ps -ef|grep http|grep -v grep`
if [ "$?" != "0" ];then
echo ">>>>no httpd,run it"
service httpd start
else
echo ">>>>httpd is runing..."
fi
sleep 5
done
#!/bin/bash
LOG_FILE="autostart.log"
#检测nginx
curtime=$(date "+%Y-%m-%d %H:%M:%S")
pnginx=`ps -ef | grep nginx | grep -v "grep" | wc -l`
if [ $pnginx -eq 0 ]; then
echo "$curtime 系统检测到nginx,已挂掉,启动中...." >> autostart.log;
/usr/sbin/nginx #启动nginx命令
echo "$curtime nginx启动完成" >> autostart.log;
else
echo "$curtime 系统检测到nginx运行正常" >> autostart.log;
fi
#检测svn
psvn=`ps -ef | grep svnserve | grep -v "grep" | wc -l`
if [ $psvn -eq 0 ]; then
echo "$curtime 系统检测到svn已挂掉,启动中...." >> autostart.log;
svnserve -d -r /home/svn/
echo "$curtime svn启动完成" >> autostart.log;
else
echo "$curtime 系统检测到SVN运行正常" >> autostart.log;
fi
#检测禅道
pzbox=`ps -ef | grep zbox | grep -v "grep" | wc -l`
if [ $pzbox -eq 0 ]; then
echo "$curtime 系统检测到禅道已挂掉,启动中...." >> autostart.log;
/opt/zbox/zbox start
echo "$curtime 禅道启动完成" >> autostart.log;
else
echo "$curtime 系统检测到禅道运行正常" >> autostart.log;
fi