Back to Unix_Utilities

See Also rsyncincron

cron

cron - daemon to execute scheduled commands

The software utility Cron is a time-based job scheduler in Unix-like computer operating systems.

通常,crontab储存的指令被守护进程激活,crond常常在后台运行,每一分钟检查是否有预定的作业需要执行。这类作业一般称为cron jobs。

1. CRON expression

# 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

# 每天凌晨2:22执行
22 2 * * * sh /data/daily_report/sendmail.sh

# 每分钟执行一次
*/1 * * * * sh /usr/local/zabbix/scripts/sdk-agent-data.sh

2. Usage

2.1. crontab

# list
crontab -l
# edit
crontab -e
1 0 * * * curl -X POST http://localhost:8081/context/add/0/
1 0 * * * sh  /srv/app/active.sh

2.2. crond

# /etc/init.d/crond status
crond (pid  1907) is running...
# tail -f /var/log/cron

2.3. debugging crontab jobs

https://stackoverflow.com/questions/4883069/debugging-crontab-jobs

# 查看cron执行异常信息
$ sudo tail -f /var/log/messages
# 调整执行速度为1分钟执行一次,并且把异常信息打入本地日志
$ crontab -l
*/1 * * * * sh /data/sdk-agent-data/daily_logs/send_report.sh > /data/sdk-agent-data/daily_logs/cron.log 2>&1

3. Reference

MainWiki: Cron (last edited 2017-06-13 15:26:26 by twotwo)