首页 > 代码库 > Linux-No.03 Linux 设置定时任务发送邮件功能

Linux-No.03 Linux 设置定时任务发送邮件功能

1、定时任务

crontab -l
crontab -e 

/sbin/service crond start    //启动服务
/sbin/service crond stop     //关闭服务
/sbin/service crond restart  //重启服务
/sbin/service crond reload   //重新载入配置
/sbin/service crond status   //查看服务状态 

*/1 * * * * /opt/shell/load-check.sh
00 03 * * * /opt/shell/download_log.sh
* */1 * * * /opt/shell/diskfree.sh

ps -ax | grep cron

2、发送邮件

#!/bin/bash
percent=`df -k | grep -v Filesystem| awk ‘{print int($5)}‘`
for each_one in $percent
do
        if [ $each_one -ge 90 ];then
                echo $each_one
                email_date=$(date "+%Y-%m-%d %H:%M:%S")
                echo "email_date : "$email_date >> /opt/disk.log
                echo "server : 120.132.38.102" >> /opt/disk.log
                df -h >> /opt/disk.log
                from_name="shaohua"
                from="kaka02@17guagua.com"
                r1="mobilewebsvr@17guagua.com"
                to="<$r1>"

                #email_title="120.132.38.102 Disk Critical"
                email_content="/opt/disk.log"
                email_subject="120.132.38.102 Disk Critical"
                echo -e "To:  ${to}\nFrom: \"${from_name}\" <${from}>\nSubject: ${email_subject}\n\n`cat ${email_content}`" | /usr/sbin/sendmail -t
                rm -rf /opt/disk.log
                break
        fi
done

Linux-No.03 Linux 设置定时任务发送邮件功能