Linux-学习之增强篇-crontab

1、简介

Linux自带的定时任务命令

Linux crontab 高级教程系列 定时任务

2、推荐指数

1
🌟🌟🌟

3、基本介绍

定时去执行指定的程序。

首先,先查看一下命令基础使用规则:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost ~]#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

结合上图,可以语法糖:分钟 小时 日期 月份 星期 [用户] 命令

4、参数详解

crontab的参数很少。

-e

编辑任务,已vim的方式打开crontab的任务列表,可以进行新增、编辑、删除等任务操作。

-l

查看任务,相当于cat 任务表。

5、符号说明

代表全部,默认*。

/

代表除,符合能被后面等数整除的条件,例如:/2

代表范围,8-12包含:8、9、10、11、12

,(英文)

代表指定的数,1,10,22,只有在1,10,22才执行

6、常用命令

每小时的第5和第10分钟执行command

1
5,10 * * * * command

每隔5min执行command

1
*/5 * * * * command

每隔1小时执行command

1
* */1 * * * command

8点的第5分钟执行command:

1
5 8 * * * command

8-11点的第5和10分钟执行command:

1
5,10 8-11 * * * command

每周一8-11点的第5和10分钟执行command:

1
5,10 8-11 * * 1 command

每月1,5,10号的8-11点的第5和10分钟执行command:

1
5,10 8-11 1,5,10 * 1 command

7、特别说明

crontab很简单,但是很实用,欢迎大家一起交流share。

Crontab指定用户运行任务


Linux-学习之增强篇-crontab
http://pygo2.top/articles/40314/
作者
mingliang.gao
发布于
2020年4月1日
许可协议