#!/bin/sh#本指令碼為守護進程, 如果發現進程僵死時, 自動重啟進程 function check_and_kill_and_start(){log_file=$1ps_name=$2dead_duration=$3# echo "check the progress "$ps_nameymd=$(date +%Y-%m-%d)log_file=${log_file}${ymd}.log# echo $log_file 判斷是否存在log_fileif [ ! -e ${log_file} ];thenexit 0fi# %y 上次修改時間last_modify=$(stat -c %Y ${log_file})now=$(date +%s)#如果上次寫入日誌的時間和現在的時間差大於 dead_duration 秒, 則重啟進程if [ $[ $now - $last_modify ] -gt $dead_duration ]; thenmail_subject='[警示][${ps_name}進程重啟]'enter='\<br\>'mail_content='${ps_name}進程重啟${enter}'echo '========================='$(date +"%Y-%m-%d %H:%M:%S")'====================================='echo [$log_file]' last update time ['$(date -d @$last_modify "+%Y-%m-%d %H:%M:%S")'], need restart progress '[$ps_name]proc_id=$(ps -ef|grep -i ${ps_name}|grep -v "grep"|awk '{print $2}')#echo 'the old ps is '${proc_id[@]}if [[ -n $proc_id ]];thenecho 'old progress exist, kill it first'for id in ${proc_id[*]}do# thread=$(ps -mp ${id}|wc -l) #殺掉父進程和子進程kill -TERM ${id} if [ $? -eq 0 ];thenecho "kill the progress sucessfull"elseecho "kill the progress failed "fidoneecho 'sleep l0s then start new progress'fi sleep 10s/usr/local/bin/python27 /var/disk/web/gsm_project/score/${ps_name} &if [ $? -eq 0 ];thenproc_id=$(ps -ef|grep -i ${ps_name}|grep -v "grep"|awk '{print $2}')echo 'the new progress id is '${proc_id[@]}elseecho "progress start failed, the cron will try to start it next time "fi#else#echo '========================='$(date +"%Y-%m-%d %H:%M:%S")'====================================='#echo [$log_file]' last update time ['$(date -d @$last_modify "+%Y-%m-%d %H:%M:%S")']'fi} #echo '========================='$(date +"%Y-%m-%d %H:%M:%S")'====================================='#執行方法 log檔案路徑 檔案名稱 尋找時間check_and_kill_and_start /var/disk/logs/score_cache/server_gsm_score score_cache_match_5se.py 30
Linux 簡單shell建立自己的守護進程,自動重啟,監控進程運行
對於守護進程管理、自動重啟、記錄log,有一個很好用的進程管理工具supervisord。它可以說簡單易用,且功能強大。但是對於我的部署需求來說還是過於繁瑣,且沒有搞定用它如何記錄進程狀態。
今天寫了一個簡單的shell指令碼,以滿足我的所有需求,並且部署簡單。
希望能夠給有同樣需求的碼農們提供一個參考。
pgmctl.sh#!/bin/bashCMD="./mypgm"PID="./log/PID.txt"LOG="./log/mypgm.log"DEBUG="false"# ---------------------------------------------------# 啟動函數function start { $CMD -debug=$DEBUG server.ini >> $LOG 2>&1 & mypgmpid=$! echo $mypgmpid > $PID echo "start [ok]"}# 停止函數function stop { kill `cat $PID` rm $PID echo "stop [ok]"}# --------------------------------------------------echo "$CMD $1"case "$1" instart) start;;start_debug) DEBUG="true" start;;restart) if [ -f $PID ] ; then stop sleep 4 fi start;;stop) stop exit 0;;esacfor (( c=0 ; ; c++ ))do if [ -f $PID ] ; then mypgmpid=`cat $PID` cmdex="ps uh -p$mypgmpid" psrtn=`$cmdex` if [ -z "$psrtn" ]; then # 進程掛掉自動重啟 echo "`date '+%Y/%m/%d %H:%M:%S'` FATALERROR RESTART SERVICE" >> $LOG start elif (( $c%20 == 0 )); then # 記錄進程運行狀態 echo "`date '+%Y/%m/%d %H:%M:%S'` PSINFO $psrtn" >> $LOG c=0 fi sleep 3 else break fidone
啟動:./pgmctl.sh start &
debug 啟動 : ./pgmctl.sh start_debug &
重啟:./pgmctl.sh restart &
停止:./pgmctl.sh stop
此指令碼只是樣本,提供一個思路。到你正在用的時候,需要按照自己需求修改