如何正确使用crontab

跑定时自己给跑死的案例算是常见吧。

这里总结了几点

cat /etc/crontab 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=""
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed

## 红包到活期账户队列 by who ##
*/10 * * * * root /usr/bin/flock -xn /data/cronlock/hbsend.lock -c 'curl -s "http://cli.libai.com/url/"'  

1.MAILTO=""
2.命令绝对路径
3.用户级别crontab -e 编写,有语法检查
4.假如一个每分钟执行的定时任务,上一分钟的 A 请求还没退出,下一分钟的 B 请求也启动了,就会导致出现 AB 同时请求的情况,所以除非跟开发确认,代码有判断,不会同时执行,否则还是适用LINUX的锁机制来处理,另外,不建议把lock文件放到/tmp目录下。
5.如果A请求一直在执行,后续请求也无法执行了,可以通过超时机制

*/10 * * * * root timeout -s SIGINT 7200 /usr/bin/flock -xn /data/cronlock/hbsend.lock -c 'curl -s "http://cli.libai.com/url/"'

猜你喜欢

转载自my.oschina.net/longquan/blog/1797394
今日推荐