检测进程是否结束并在结束时发送一份邮件

版本1

check.sh:

#!/bin/bash
source .bash_profile
i=`ps -ef | grep /usr/local/PostgreSQL/9.6/data/ | grep -v grep | wc -l`
if [ $i -lt 1 ]
then
text='process cannot find. The upgrade job is done.please help to vacuum'
echo "$text" | /bin/mail -s "192.168.15.32 pg_upgrade" barry.chen@coresolutions.com,andy.wong@coresolutions.com,murphy.leung@coresolutions.com
fi


但发现一个问题:
-bash: mail: command not found
呵呵,显然mailx没有安装,于是:

yum -y install mailx

把check.sh 设置crontab ,每一分钟跑一次

*/1 * * * * /root/check.sh > /dev/null 2>&1

发现版本1中要进程不存在时发完一封邮件后会,接下来的每一分钟会不断的发一封。

修改脚本为当检测进程不存在时发完一封邮件后,下次的每一分钟停止发送

版本2

check2.sh

#!/bin/bash
source .bash_profile
TEMP_FOLDER=/root
i=`ps -ef | grep /var/lib/pgsql96/data/ | grep -v grep | wc -l`


generateDoneFile() {
        echo `date "+%Y-%m-%d %H:%M %z %Z"`>${TEMP_FOLDER}/$1.done
        }


if [ -e ${TEMP_FOLDER}/sendemail.done ];
        then
        echo "already sendemail"
        exit 10
else
        if [ $i -lt 1 ]
        then
        text='process cannot find. The upgrade job is done.please help to vacuum'
        echo "$text" | /bin/mail -s "192.168.15.30 ECI pg_upgrade" barry.chen@coresolutions.com
        generateDoneFile sendemail
        fi
fi

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值