您的位置:首页 > 其它

How to trigger automatic job by Cron

2017-03-16 11:20 351 查看
Cron是类unix系统(Linux,Mac,etc.)标配的定期执行命令的自动化工具,每个系统用户都有自己的cron服务,需要定期执行的命令记录在以用户名命名的crontab文件里,各个Crontab文件统一存放在/var/spool/cron/crontabs/文件夹下。

root@bruce-virtual-machine:/var/spool/cron/crontabs# ls -al
total 16
drwx-wx--T 2 root  crontab 4096 3月  22 21:27 .
drwxr-xr-x 3 root  root    4096 7月  23  2014 ..
-rw------- 1 bruce crontab 1087 3月  22 21:27 bruce
-rw------- 1 root  crontab 1087 3月  22 21:25 root


实质上,Cron是一个系统级别的daemon。

root@bruce-virtual-machine:~# ps -ef | grep cron
root       740     1  0 21:19 ?        00:00:00 /usr/sbin/cron -f
root      3950  3055  0 22:13 pts/11   00:00:00 grep --color=auto cron

root@bruce-virtual-machine:~# service cron status
● cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2017-03-22 21:19:04 CST; 54min ago
Docs: man:cron(8)
Main PID: 740 (cron)
CGroup: /system.slice/cron.service
└─740 /usr/sbin/cron -f


它的最小监测时间单位是分钟,因此它每分钟都会读取并检测各个系统用户的crontab文件,然后在匹配的时间点以对应用户的身份触发shell并执行命令,以此实现简单的自动化流程,如果编辑完Crontab文件发现并没有按时执行命令可以使用
service cron restart
重启服务。

使用Cron除了配置crontab文件之外无需其他操作,实现简单方便,因此在独立服务器维护,定时任务触发业务应用广泛,但功能拓展性不强,很难构建大型分布式的自动化系统比如软件开发领域的CI系统,在该领域Jenkins等专业的CI server是首选。

Crontab文件是Cron配置中的关键所在,但在实际的操作中很少会直接用文本编辑器编辑该文件,因为配备Cron的系统(包括Mac)都会提供Crontab工具来简化文件的编辑。关于crontab命令的详细参数可以
man crontab
查看, 在日常操作中使用频率较多两个参数是
-e
-l


The -l option causes the current crontab to be displayed on standard output.

The -e option is used to edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables. After you exit from the editor, the modified crontab will be installed automatically.

使用
crontab -e
编辑定时命令,使用
crontab -l
查看定时命令,掌握这两条指令就可以完成绝大多数的crontab操作,因为crontab配置的重点在于定时命令的编辑。

Crontab定时命令的格式如下:

# 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


前五个field定义时间点,最后一个field定义命令。

A field may be an asterisk (*), which always stands for “first-last”.

Ranges of numbers are allowed. Ranges are two numbers separated with a hyphen. The specified range is inclusive. For example, 8-11 for an “hours” entry specifies execution at hours 8, 9, 10 and 11.

Lists are allowed. A list is a set of numbers (or ranges) separated by commas. Examples: “1,2,5,9”, “0-4,8-12”.

Step values can be used in conjunction with ranges. Following a range with “” specifies skips of the number’s
bdd7
value through the range. For example, “0-23/2” can be used in the hours field to specify command execution every other hour (the alternative in the V7 standard is “0,2,4,6,8,10,12,14,16,18,20,22”). Steps are also permitted after an asterisk, so if you want to say “every two hours”, just use “*/2”.

Names can also be used for the “month” and “day of week” fields. Use the first three letters of the particular day or month (case doesn’t matter). Ranges or lists of names are not allowed.

The “sixth” field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile.

These special time specification “nicknames” are supported, which replace the 5 initial time and date fields, and are prefixed by the ‘@’ character:

@reboot : Run once after reboot.

@yearly : Run once a year, ie. “0 0 1 1 *”.

@annually : Run once a year, ie. “0 0 1 1 *”.

@monthly : Run once a month, ie. “0 0 1 * *”.

@weekly : Run once a week, ie. “0 0 * * 0”.

@daily : Run once a day, ie. “0 0 * * *”.

@hourly : Run once an hour, ie. “0 * * * *”.

reference from crontab(5) - Linux man page

Example

#FTP Server
[lterbsbj]
*/20 7-23 * * * /local_stg/tools/lterbsbj_tools/daily_sync_proj>>~/sync_up_1.log
*/20 * * * * /local_stg/tools/lterbsbj_tools/daily_sync_proj_all>>~/sync_up_1.log
0 9 * * 1-5 rsync -avz /proj/lterbsSwdi/usr/edonxio/gte/* /local_stg/lterbsFtp_up/gte/ >>~/sync_gte.log
0 9 * * 1-5 rsync -avz /proj/lterbsFtp_up/up/node_backup/bjenb* /local_stg/lterbsFtp_up/up/node_backup/. >>~/node_backup.log
0 9 * * * source /vobs/erbs/int/target.cshrc; /mnt/shared/tools/lterbsbj_tools/dailybuild_check.pl > /mnt/shared/dailytest/ft_dailytest.html; /mnt/shared/tools/lterbsbj_tools/dailybuild_check.pl -mail
0 0 * * 1 find /local_stg/lterbsFtp_up/blk_up -noleaf -maxdepth 1 -mtime +30 -type d |xargs -i rm -fr {}
0 0 * * 1 find /local_stg/lterbsFtp_up/up -noleaf -maxdepth 1 -mtime +30 -type d -name "CXP*" | xargs -i rm -fr {}
0 0 * * * rsync -avz /proj/lterbsSwdiTools/ipad/web/ipad/lib /local_stg/tools/ipad/.
@reboot sleep 5m; /usr/bin/vmrun  -T ws start /local_stg/vmware/SUSE\ Linux\ Enterprise\ 11\ 32bit/SUSE\ Linux\ Enterprise\ 11\ 32bit.vmx nogui &
@reboot sleep 5m; /usr/bin/vmrun  -T ws start /local_stg/vmware/bjgte11/SUSE\ Linux\ Enterprise\ 11\ 32bit.vmx nogui &
[root]
0 8 4 10 * shutdown -h
*/15 * * * * [ `tail -n 3 /tmp/logs/sentry_telnet_server.log | grep Broken | wc -l` = 0 ] || ps -ef | grep sentry | grep -v grep | awk '{print $2}' | xargs kill -9
0 * * * * SHELL=/bin/ksh ;  ps -ef | grep sentry | grep -v grep | awk '{print $2}' | xargs -iPID kill -9 PID; /local_stg/tools/lterbsbj_tools/sentry_telnet_server.py > /tmp/logs/sentry_telnet_server.log 2>&1 &
0 9 * * 1  find /local_stg/lterbsFtp_up/up -noleaf -maxdepth 1 -mtime +30 -type d -name "CXP*" | xargs -i rm -fr {}
30 15 * * * for ARG in `ls -1d /proj/lterbs*`; do umount $ARG ;  sleep 1 ; mount $ARG ; done
0 23 * * * /bin/mount -a
30 08 * * * /home/lterbsbj/ejqizng/up_list.pl >> /home/lterbsbj/ejqizng/up_list.log
30 08 * * * /home/lterbsbj/ejqizng/up_list_g2.pl >> /home/lterbsbj/ejqizng/up_list_g2.log
0 09 * * * /local_stg/tools/lterbsbj_tools/up_list.pl
0 21 * * 6  find /local_stg/lterbsFtp_up/up -noleaf -maxdepth 1 -mtime +90 -type d -name "CXP102051*" | xargs rm -fr >> ~/updelete.log
0 21 * * 6 /root/ejqizng/up_list_clear.sh >> /root/ejqizng/up_list_clear.log
@reboot sleep 5m; /bin/mount -a
#WatchTower
[root]
0 2 * * * /etc/cron.daily/suse.de-clean-tmp >>/root/clean-tmp.log
10 1 * * * /etc/nxserver/daily_clean.sh >> daily_clean.log
[ltebjdt]
30 * * * * /proj/tgf/dailytest/worker/start_erl_tower wonder_woman Bj >/dev/null 2>&1
*/3 * * * * /proj/tgf/dailytest/worker/decoupled_filetransfer.sh >~/decoupled_filetransfer.log 2>&1
0 */4 * * * /proj/tgf/dailytest/worker/maintenance.sh    >/proj/tgf/dailytest/log/maintenance.log 2>&1
0 3 * * * /proj/tgf/dailytest/worker/maintenance.sh d  >/proj/tgf/dailytest/log/maintenance_daily.log 2>&1
*/5   * * * * ~/dailytest/agents/artie/artie >/dev/null 2>&1
*/10  * * * * ~/dailytest/agents/artie/watch-artie >/dev/null 2>&1
0,20,40 1-23 * * * /proj/daily_sync_proj >>~/sync_up.log
#Worker
[lterbsbj]
45 16 * * * source /home/lterbsbj/target_dt.cshrc; /mnt/shared/tools/lterbsbj_tools/TGF_dailytest_899_ET2.pl >> /home/lterbsbj/log/tgf_dailytest_899_ET2.log
40 16 * * * source /home/lterbsbj/target_dt.cshrc; /mnt/shared/tools/lterbsbj_tools/TGF_dailytest_up_899_ET2.pl > /home/lterbsbj/log/tgf_dailytest_up_899_ET2.log
45 8 * * * source /home/lterbsbj/target_dt.cshrc; /mnt/shared/tools/lterbsbj_tools/TGF_dailytest_899.pl >> /home/lterbsbj/log/tgf_dailytest_899_ET2.log
30 8 * * * source /home/lterbsbj/target_dt.cshrc; /mnt/shared/tools/lterbsbj_tools/TGF_dailytest_up_899.pl > /home/lterbsbj/log/tgf_dailytest_up_899_ET2.log
0 2 * * * source /home/lterbsbj/target_dt.cshrc; tgactivity /home/lterbsbj/ejqizng/bjlmrenb1089_FT.spec 89_R10X > /home/lterbsbj/ejqizng/bjlmrenb1089_FT.log
0 16 * * * tgactivity /home/lterbsbj/dt_bj_lsv_8910ft_bjenb03.spec 89_R10CY > /home/lterbsbj/bjenb03.log
0 16 * * * tgactivity /home/lterbsbj/dt_bj_lsv_8910ft_bjlmrenb1089.spec 89_R10CY > /home/lterbsbj/bjlmrenb1089.log
0 * * * * Source /home/lterbsbj/target_dt.cshrc; /mnt/shared/tools/lterbsbj_tools/TGF_dailytest_899.pl >> /home/lterbsbj/tgf_dailytest_899.log
[root]
0 * * * * SHELL=/bin/ksh ;  ps -ef | grep sentry | grep -v grep | awk '{print $2}' | xargs -iPID kill -9 PID; /home/lterbsbj/eqenyyu/tool/sentry_telnet_server.py >> /home/lterbsbj/eqenyyu/sentry_telnet_server.log 2>&1 &


/var/log/syslog
里查看Cron消息。

root@bruce-virtual-machine:/var/log# cat syslog | grep cron
Mar 22 21:24:33 bruce-virtual-machine crontab[2954]: (root) BEGIN EDIT (root)
Mar 22 21:25:12 bruce-virtual-machine anacron[834]: Job `cron.daily' terminated
Mar 22 21:25:30 bruce-virtual-machine crontab[2954]: (root) REPLACE (root)
Mar 22 21:25:30 bruce-virtual-machine crontab[2954]: (root) END EDIT (root)
Mar 22 21:27:04 bruce-virtual-machine crontab[3024]: (bruce) BEGIN EDIT (bruce)
Mar 22 21:27:16 bruce-virtual-machine crontab[3024]: (bruce) REPLACE (bruce)
Mar 22 21:27:16 bruce-virtual-machine crontab[3024]: (bruce) END EDIT (bruce)
Mar 22 21:29:09 bruce-virtual-machine anacron[834]: Job `cron.weekly' started
Mar 22 21:29:09 bruce-virtual-machine anacron[3075]: Updated timestamp for job `cron.weekly' to 2017-03-22
Mar 22 21:31:28 bruce-virtual-machine anacron[834]: Job `cron.weekly' terminated
Mar 22 21:34:09 bruce-virtual-machine anacron[834]: Job `cron.monthly' started
Mar 22 21:34:09 bruce-virtual-machine anacron[834]: Job `cron.monthly' terminated
Mar 22 21:34:09 bruce-virtual-machine anacron[834]: Normal exit (3 jobs run)
Mar 22 22:01:37 bruce-virtual-machine crontab[3272]: (root) LIST (root)
Mar 22 22:17:01 bruce-virtual-machine CRON[3967]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: