使用shell脚本编写程序定时查询数据库,根据数据库结果发送短信和邮件。定时任务使用crontab实现,具体实现如下。
1. crontab 定时执行任务
crontab -e进行编辑,使用方法类似于vi,
例子:0 * * * * bash /home/work/opt/nginx/sbin/nginx_log.sh
2. 完整脚本
#!/bin/bash
if [ $# -lt 2 ]
then
echo "params error";
exit;
fi
#创建日志文件
LOG_FILE_NAME="/home/work/monitor/xxx"$1".log"
if [ ! -e "$LOG_FILE_NAME" ]
then
touch $LOG_FILE_NAME;
fi
LOG=$(date "+%Y-%m-%d %H:%M:%S");
#当前时间
current_time=$(date +%s);
echo $current_time
#查询开始时间
start_time&#