29 Dec 2014
作用:维护每个独立用户的crontab文件
安装:yum install cronie -y
服务启用:
service crond start
chkconfig crond on
配置文件:/etc/crontab
配置文件详情可查看man,里面有很多语法示例
man 5 crontab
日志文件:/var/log/cron
参数:
-u - 指定用户,默认指定执行命令的用户-l - 列出指定用户的计划任务-e - 编辑指定用户的计划任务参数用法举例:
# 查看就指定用户的计划任务 crontab -l no crontab for root crontab -u test -l no crontab for test # 编辑指定用户的计划任务 crontab -e ****************************************** # 跟vi的操作一样 # 在这里按照格式填写计划任务 ******************************************
语法:
crontab [-u user] file
crontab [-u user] [-l | -r | -e] [-i] [-s]
## 文件内容格式 # 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 分钟 小时 日 月 星期几 用户名 命令
语法举例
## 时间点:每隔8小时 0 0,8,16 * * * 0 */8 * * * ## 时间段:每天9-18点 0 9-18 * * * ## 每个月第一个周六 需求: 今天朋友跟我讨论了一个问题,crontab的语法是"分 时 日 月 周 command",那如何实现在每个月的第一个周六来执行某个命令或脚本呢? 解决方案: 1、先做个每周六执行的计划; 2、计划里执行一个脚本,脚本的内容是判断data +%e的执行结果,如果小于7的话,意味着这是当前月的第一个周,就执行需要执行的命令或脚本。 每个月的第一个周六: 0 0 * * 6 运行你的脚本 脚本里内容是 ******************************** #!/bin/bash t=$(date +%e) if [ "$t" -lt 7 ]; then 你需要执行的命令; fi ******************************** ## 每10s执行 需求: crond的基本语法最小按照分来执行,但遇到了一种同步文件的需求,需要ftp不同目录间文件同步,10秒执行一次同步 解决方案: *********************************** * * * * * /usr/bin/expect /root/ftpsync.sh root 172.16.2.51 * * * * * sleep 10; /usr/bin/expect /root/ftpsync.sh root 172.16.2.51 * * * * * sleep 20; /usr/bin/expect /root/ftpsync.sh root 172.16.2.51 * * * * * sleep 30; /usr/bin/expect /root/ftpsync.sh root 172.16.2.51 * * * * * sleep 40; /usr/bin/expect /root/ftpsync.sh root 172.16.2.51 * * * * * sleep 50; /usr/bin/expect /root/ftpsync.sh root 172.16.2.51 ***********************************
扩展知识:
# 系统级计划任务-配置文件 ll -d /etc/cron.* drwxr-xr-x. 2 root root 4096 Jan 13 05:55 /etc/cron.d drwxr-xr-x. 2 root root 4096 Apr 27 23:43 /etc/cron.daily -rw-------. 1 root root 0 Mar 30 2015 /etc/cron.deny drwxr-xr-x. 2 root root 4096 Sep 27 2011 /etc/cron.hourly drwxr-xr-x. 2 root root 4096 Sep 27 2011 /etc/cron.monthly drwxr-xr-x. 2 root root 4096 Sep 27 2011 /etc/cron.weekly # 此文件夹下都是系统级的任务 ls -l /etc/cron.d total 12 -rw-r--r--. 1 root root 113 Nov 23 2013 0hourly -rw-r--r--. 1 root root 108 Oct 11 2013 raid-check -rw-r--r-- 1 root root 229 Nov 23 2013 sysstat # run-parts命令是在每分钟由root用户身份执行/etc/cron.hourly目录下的所有可执行文件 cat /etc/cron.d/0hourly SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ 01 * * * * root run-parts /etc/cron.hourly # 每十分钟监测一次系统信息,每天23:53保存一次 cat /etc/cron.d/sysstat # Run system activity accounting tool every 10 minutes */10 * * * * root /usr/lib/sa/sa1 1 1 # 0 * * * * root /usr/lib/sa/sa1 600 6 & # Generate a daily summary of process accounting at 23:53 53 23 * * * root /usr/lib/sa/sa2 -A # 在每个周日1:00am运行一次raid-check cat /etc/cron.d/raid-check # Run system wide raid-check once a week on Sunday at 1am by default 0 1 * * Sun root /usr/sbin/raid-check # "/var/spool/cron/"Crontab命令创建的针对不同用户的配置 ll /var/spool/cron/ total 0 -rw------- 1 root root 0 May 19 16:59 root