Overview of timed polling of databases and sending HTTP requests via Linux timed tasks
有时需要临时增加一个定时任务(需要根据数据库查询结果然后发送HTTP请求),如果在项目中额外增加(Java+Spring+Quartz),则需要编写很多代码,而且还需要重新编译发布,比较麻烦,特别是在紧急情况下的时候。采用Linux脚本,再加上JSP(根据业务需要),可以比较快的解决问题。
Environment preparation
- Install MySQL Client
- Installing Curl
Script Content
#!/bin/bash## @file test_task.sh# @brief test_task# @author huligong1234# @version 0.1# @date 2018-02-17# #脚本目录WORK _dir=/data/scripts# Log directory log_dir=${work_dir}/logs/test_tasklog_file_name=test_task_ ' date +%Y%m%d%H '. logLOG_FILE=$ {Log_dir}/${log_file_name} #MySQL连接信息MYSQL_HOST = "192.168.1.106" mysql_user= "readonly" mysql_pwd= "ReadOnly" Mysql_ Database= "MyProj" #目标HTTP地址SENDTO_NOTIFY_URL = "http://192.168.1.106:8081/notify/notify.jsp" #SENDTO_NOTIFY_URL = " http://api.domain.com/notify/notify.jsp "#查询数据获得目标ID集合idList = ' mysql-h $MYSQL _host-u $MYSQL _user-p$mysql_pwd $MYSQL _database <<eof | Tail-n +2select t.tx_no from T_app_log twhere t.create_date between Date_sub (now (), INTERVAL 5 MINUTE) and now () and T.resu Lt_code= ' SUCCESS ' ORDER by T.create_date Desclimit 50; EOF ' #迭代集合, sending HTTP requests through curl one by one for the TID in $idList; Do echo ' date ' +%y-%m-%d%h:%m:%s ' [$tid] ' Curl-o/dev/null-s-M 3--connect-timeout 3-w%{http_code} ' ${sendto_n otify_url}?tx_no= $tid "' >> ${log_file}done
Crontab configuration (performed once per minute)
$ crontab -e #编辑$ crontab -l #查看
0 0 * * * /bin/bash /usr/local/tengine/logs/cut-log.sh*/1 * * * * /bin/bash /data/scripts/test_task.sh00 12 * * * /sbin/ntpdate cn.pool.ntp.org
Configure for boot start
方法一、修改/etc/rc.local方法二、chkconfig管理 (/etc/init.d)
Note
Timed polling of databases and sending HTTP requests through Linux timed tasks