1. 程式人生 > >shell-檢測服務是否運行,並記日誌

shell-檢測服務是否運行,並記日誌

cron 是否 post 分鐘 else shel 當前時間 服務 stat

目的:每隔*分鐘檢測服務是否運行;若運行中,則記錄執行的進程名稱;若不運行,記錄當前時間

shell:

#!/bin/bash

date=`date +%Y%m%d`
log=/home/mono_${date}.log
err_log=/home/ERR_${date}.log

if [ ! -f ${log} ]; then
    touch ${log}
fi

if [ ! -f ${err_log} ]; then
    touch ${err_log}
fi

PATH=PATH:/sbin:/bin:/usr/bin

cmd=`ps -ef | grep mono | grep -v grep | wc -l`

if [ ${cmd} -eq 1 ]; then
    date >> ${err_log}
else
    date >> ${log}
    ps -ef | grep mono >> ${log}
    echo "*************************" >> ${log}
fi

crontab -e

*/5 * * * * /bin/sh /home/mono_stat.sh

shell-檢測服務是否運行,並記日誌