温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

定时检查进程存在情况

发布时间:2020-07-31 17:00:36 来源:网络 阅读:297 作者:taijihua 栏目:移动开发
用shell脚本实现每隔30s检查httpd进程存在与否,httpd存在时输出0,不存在输出1.

方法一:

单条命令实现
cat apache.sh
#! /bin/bash
while true
do
ps -ef | grep http | grep -v grep > /dev/null  && echo 0 || echo 1
sleep 30
done

while true为真,一直执行do循环。
# ps -ef | grep http ,过滤出http进程
输出结果:
root      7286     1  0 15:14 ?        00:00:00 /usr/sbin/httpd
nagios    7288  7286  0 15:14 ?        00:00:00 /usr/sbin/httpd
nagios    7289  7286  0 15:14 ?        00:00:00 /usr/sbin/httpd
nagios    7290  7286  0 15:14 ?        00:00:00 /usr/sbin/httpd
nagios    7291  7286  0 15:14 ?        00:00:00 /usr/sbin/httpd
nagios    7292  7286  0 15:14 ?        00:00:00 /usr/sbin/httpd
nagios    7293  7286  0 15:14 ?        00:00:00 /usr/sbin/httpd
nagios    7294  7286  0 15:14 ?        00:00:00 /usr/sbin/httpd
nagios    7295  7286  0 15:14 ?        00:00:00 /usr/sbin/httpd
root      7440  4708  0 15:17 pts/0    00:00:00 grep http
# ps -ef | grep http | grep -v grep,过滤ps -ef |grep http本身。
输出结果:
root      7286     1  0 15:14 ?        00:00:00 /usr/sbin/httpd
nagios    7288  7286  0 15:14 ?        00:00:00 /usr/sbin/httpd
nagios    7289  7286  0 15:14 ?        00:00:00 /usr/sbin/httpd
nagios    7290  7286  0 15:14 ?        00:00:00 /usr/sbin/httpd
nagios    7291  7286  0 15:14 ?        00:00:00 /usr/sbin/httpd
nagios    7292  7286  0 15:14 ?        00:00:00 /usr/sbin/httpd
nagios    7293  7286  0 15:14 ?        00:00:00 /usr/sbin/httpd
nagios    7294  7286  0 15:14 ?        00:00:00 /usr/sbin/httpd
nagios    7295  7286  0 15:14 ?        00:00:00 /usr/sbin/httpd
# ps -ef | grep http | grep -v grep > /dev/null,输出到空设备文件。

# ps -ef | grep http | grep -v grep > /dev/null  && echo 0 || echo 1
逻辑与:&&,逻辑或:||。"ps -ef | grep http | grep -v grep > /dev/null"为真时执行echo 0,否则执行echo 1.

方法二:
cat apache.sh
while true
httpnum=`ps -ef | grep http | grep -v grep| wc -l`
do
    if [ $httpnum -gt 0 ]
    then 
	echo 0
    else
	echo 1
    fi
sleep 30
done

方案二摘自老男孩博客http://oldboy.blog.51cto.com/2561410/577227,里面有详细介绍。


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI