1.crontab
分时日月周
[root@niejicai-linux ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
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
[
root@niejicai-linux ~]#
4. Linux系统任务计划
/etc/crontab cron的主配置文件,可以定义PATH
cron格式如下:
# .----------------分钟 (0 - 59)
# | .------------- 小时 (0 - 23)
# | | .---------- 日 (1 - 31)
# | | | .------- 月 (1 - 12)
# | | | | .---- 周 (0 - 6) (周日=0 or 7)
# | | | | |
# * * * * * user-name command to be executed
cron 也是一个服务,所以需要先启动服务才能生效:service crond start; service crond status
任务计划练习题:
1.每天凌晨1点20分清除/var/log/slow.log这个文件
[root@niejicai-linux ~]# crontab -e
20 1 * * * echo "0"> /var/log/slow.log
2.每周日3点执行 “/bin/sh /usr/local/sbin/backup.sh”
[root@niejicai-linux ~]# crontab -e
0 3 * * 0 /bin/bash /usr/local/sbin/backup.sh
3.每月14号4点10分执行 “/bin/sh /usr/local/sbin/backup_month.sh”
[root@niejicai-linux ~]# crontab -e
10 4 14 * * /bin/bash /bin/sh /usr/local/sbin/backup_month.sh
4.每隔8小时执行 “ntpdate time.windows.com”
[root@niejicai-linux ~]# crontab -e
方法一:
0 0,8,16 * * * ntpdate time.windows.com
方法二:
0 */8 * * * ntpdate time.windows.com
5.每天的1点,12点,18点执行 “/bin/sh /usr/local/sbin/test.sh”
[root@niejicai-linux ~]# crontab -e
* 1,12,18 * * * /bin/bash /bin/sh/usr/local/sbin/test.sh
6.每天的9点到18点执行 “/bin/sh /usr/local/sbin/test2.sh”
[root@niejicai-linux ~]# crontab -e
* 9-18 * * * /bin/bash /bin/sh /usr/local/sbin/test2.sh
[root@niejicai-linux ~]# anacron简单说吧,crontab 可以指定任务在每天几点钟运行,可是如果那个钟点机器没有开,
那个任务便错过了时间在一个新的时间轮回之内不再运行了。(详细可以查看资料)!!
那个任务便错过了时间在一个新的时间轮回之内不再运行了。(详细可以查看资料)!!
[root@niejicai-linux ~]# ls /etc/anacrontab
/etc/anacrontab
[
root@niejicai-linux ~]# cat !$
cat /etc/anacrontab
# /etc/anacrontab: configuration file for anacron
# See anacron(8) and anacrontab(5) for details.
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
#period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly
[
root@niejicai-linux ~]#