新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
定時(shí)檢查進(jìn)程存在情況
用shell腳本實(shí)現(xiàn)每隔30s檢查httpd進(jìn)程存在與否,httpd存在時(shí)輸出0,不存在輸出1. 方法一: 單條命令實(shí)現(xiàn) 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為真,一直執(zhí)行do循環(huán)。 # ps -ef | grep http ,過(guò)濾出http進(jìn)程 輸出結(jié)果: 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,過(guò)濾ps -ef |grep http本身。 輸出結(jié)果: 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,輸出到空設(shè)備文件。 # ps -ef | grep http | grep -v grep > /dev/null && echo 0 || echo 1 邏輯與:&&,邏輯或:||。"ps -ef | grep http | grep -v grep > /dev/null"為真時(shí)執(zhí)行echo 0,否則執(zhí)行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,里面有詳細(xì)介紹。
本文名稱(chēng):定時(shí)檢查進(jìn)程存在情況
文章出自:http://www.dlmjj.cn/article/jeiiji.html