20151209 Linux系统日常管理 (第三部分计划任务crontab;anacron)

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
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
方法二: 
*/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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值