溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

定時(shí)檢查進(jìn)程存在情況

發(fā)布時(shí)間:2020-07-31 17:00:36 來(lái)源:網(wǎng)絡(luò) 閱讀:301 作者:taijihua 欄目:移動(dòng)開發(fā)
用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ì)介紹。


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI