crontab使用

crontab简介

No time for commands? Scheduling tasks with cron means programs can run but you don’t have to stay up late.没有时间去做命令?用cron来调度任务意味着以后你不用熬夜去换起任务。

对于某些需要在特定时刻,一次或多次执行的任务。可以用crontab方便快捷的来描述预设任务的执行时间,次数,或周期。

The crond daemon is the background service that enables cron functionality.
The cron service checks for files in the /var/spool/cron and /etc/cron.d directories and the /etc/anacrontab file. The contents of these files define cron jobs that are to be run at various intervals. The individual user cron files are located in /var/spool/cron, and system services and applications generally add cron job files in the /etc/cron.d directory. The /etc/anacrontab is a special case that will be covered later in this article.

crontab常见使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# crontab -e
SHELL=/bin/bash
MAILTO=root@example.com
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

# 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
# | | | | |
# * * * * * command parameters


# Set the hardware clock to keep it in sync with the more accurate system clock
# 每天的5调03分执行
03 05 * * * /sbin/hwclock --systohc

命令实例

.—————- 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
| | | | |
* * * * * command parameters
分 时 天 月 星期 命令 参数。 名必须是绝对路径

分 可填值

  1. 1 具体某个0-59的数字。表示第几分钟
  2. 1,2,5,7 多个枚举的数字。表示多个分钟时间点
  3. 1-30 表示一个范围,1到30分钟
  4. * 表示任意分钟
  5. */10 表示每间隔10分钟