linux進(jìn)程監(jiān)控shell腳本代碼
為確保php頁(yè)面在后臺(tái)運(yùn)行,寫(xiě)了一個(gè)監(jiān)控腳本,假設(shè)程序異常退出,那么可以自動(dòng)重啟。
shell腳本/usr/local/scripts/receve.sh內(nèi)容:
#!/bin/bash #funtion:keep receve.php running PHP="/usr/local/php5/bin/php" PROGRAM="receve.php" #start dameo $PHP /home/httpd/$PROGRAM >>/usr/local/scripts/phpshell/receve.out & chpid="$!"; echo "$chpid" > /usr/local/scripts/phpshell/receve.sid echo "child pid is $chpid" echo "status is $?" while [ 1 ] do wait $chpid exitstatus="$?" echo "child pid=$chpid is gone, $exitstatus" >>/usr/local/scripts/phpshell/receve.php_error.log echo `date` >> /usr/local/scripts/phpshell/receve.php_error.log echo "**************************" >>/usr/local/scripts/phpshell/receve.php_error.log sleep 10 $PHP /home/httpd/$PROGRAM >> /usr/local/scripts/phpshell/receve.out & chpid="$!"; echo "$chpid" > /usr/local/scripts/phpshell/receve.sid echo "next child pid is $chpid" echo "next status is $?" done
需要保證監(jiān)控腳本正常運(yùn)行,在每天凌晨自動(dòng)重啟腳本/usr/local/scripts/restart_r.sh,寫(xiě)入crontab:
#!/bin/bash echo "" >/tmp/receve_pid.txt /bin/ps ax | /bin/grep "receve.sh" | /bin/grep -v "grep" | /bin/awk '{print $1}' >/tmp/receve_pid.txt no1pid=`cat /tmp/receve_pid.txt | sort -r` no2pid=`cat /usr/local/scripts/phpshell/receve.sid | sort -r` if [ -z $no1pid ];then echo "receve.sh pid is null, receve.sh is not running." else /bin/kill -9 "$no1pid" echo "receve.sh killed" fi sleep 3 if [ -z $no2pid ];then echo "receve.php pid is null, receve.php is not running." else /bin/kill -9 "$no2pid" echo "receve.php killed" fi rm -rf /usr/local/scripts/phpshell/receve.sid sleep 3 /usr/local/scripts/receve.sh > /dev/null &
crontab:
#restart receve.sh
10 00 * * * /usr/local/scripts/restart_r.sh
注意:腳本命名要避免重復(fù),避免自己kill掉自己的進(jìn)程。
相關(guān)文章
linux shell 自定義函數(shù)方法(定義、返回值、變量作用域)
linux shell 可以用戶(hù)定義函數(shù),然后在shell腳本中可以隨便調(diào)用。下面說(shuō)說(shuō)它的定義方法,以及調(diào)用需要注意那些事項(xiàng)2014-09-09ubuntu修改terminal終端的主機(jī)名的實(shí)現(xiàn)方法
這篇文章主要介紹了ubuntu修改terminal終端的主機(jī)名的實(shí)現(xiàn)方法的相關(guān)資料,希望通過(guò)本文能幫助大家,需要的朋友可以參考下2017-08-08bash shell獲取當(dāng)前腳本的絕對(duì)路徑(pwd/readlink)
有時(shí)候,我們需要知道當(dāng)前執(zhí)行的輸出shell腳本的所在絕對(duì)路徑,本文主要介紹了bash shell獲取當(dāng)前腳本的絕對(duì)路徑,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02Shell腳本對(duì)比兩個(gè)文本文件找出不同行的2個(gè)方法分享
這篇文章主要介紹了Shell腳本對(duì)比兩個(gè)文本文件找出不同行的2個(gè)方法分享,本文分別使用comm命令法、grep命令法實(shí)現(xiàn),需要的朋友可以參考下2014-12-12linux命令行bandit使用練習(xí)實(shí)戰(zhàn)
這篇文章主要為大家介紹了linux命令行bandit使用練習(xí)實(shí)戰(zhàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Linux定時(shí)執(zhí)行任務(wù)at和crontab命令詳解
本篇文章主要介紹了Linux定時(shí)執(zhí)行任務(wù)at和crontab命令這兩個(gè)命令的基本用法和區(qū)別,一起學(xué)習(xí)下。2017-11-11利用shell刪除數(shù)據(jù)表中指定信息和字段對(duì)應(yīng)的文件
這篇文章主要介紹了利用shell刪除數(shù)據(jù)表中指定信息和字段對(duì)應(yīng)的文件,需要的朋友可以參考下2014-04-04Shell中使用plink工具實(shí)現(xiàn)遠(yuǎn)程批量關(guān)機(jī)
這篇文章主要介紹了Shell中使用plink工具實(shí)現(xiàn)遠(yuǎn)程批量關(guān)機(jī),Plink 是命令行工具,執(zhí)行遠(yuǎn)程服務(wù)器上的命令,需要的朋友可以參考下2014-12-12學(xué)習(xí)shell腳本之前的基礎(chǔ)知識(shí)[圖文]
在學(xué)習(xí)shell腳本之前,需要你了解很多關(guān)于shell的知識(shí),這些知識(shí)是編寫(xiě)shell腳本的基礎(chǔ),所以希望你能夠熟練的掌握2013-03-03