[Linux] crontab设置

crontab 常用命令:
查看定时任务 crontab -l
编辑定时任务 crontab -e

注意:如果脚本没问题,但是crontab不生效,可以查一下服务状态(Redhat/CentOS)

systemctl status crond

crontab 定时设置:

[root@xxx ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# 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

[例]
每天早上6点执行 
00 06 * * * echo "Good morning.Today is `date "+%A"`." >> /tmp/reminder.txt

每两个小时执行 
0 */2 * * * echo "Have a break now." >> /tmp/reminder.txt

晚上11点到早上7点之间每两个小时和早上8点执行
0 23-7/2,8 * * * /bin/test.sh

每个月的8号和每周的周一至周三的早上10点 
0 10 8 * 1-3 command line

每月1日和每周的周一早上7点 
0 7 1 1-12 1 echo "Make planning." >> /tmp/reminder.txt

每小时的01分钟执行/etc/cron.hourly内的脚本
01 * * * * root run-parts /etc/cron.hourly
每天4:00执行/etc/cron.daily内的脚本
0 4 * * * root run-parts /etc/cron.daily 

每星期日20:00执行/etc/cron.weekly内的脚本(注:周日可以写0或7)
0 20 * * 0 root run-parts /etc/cron.weekly 

每月21号22:23分执行/etc/cron.monthly内的脚本 
23 22 21 * * root run-parts /etc/cron.monthly 

注: run-parts命令位于/usr/bin/run-parts,内容是遍历目标文件夹的shell脚本,执行第一层目录下的有可执行权限的文件。


相关资料:
Crontab详细用法-定时任务详解  https://www.cnblogs.com/xiao-lei/p/11084578.html
Linux下的crontab定时执行任务命令详解  https://www.cnblogs.com/yanzi-meng/p/9156812.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值