crontab

创建定时任务:

 1 root@xxj-VirtualBox:~# crontab -l
 2 # Edit this file to introduce tasks to be run by cron.
 3 # 
 4 # Each task to run has to be defined through a single line
 5 # indicating with different fields when the task will be run
 6 # and what command to run for the task
 7 # 
 8 # To define the time you can provide concrete values for
 9 # minute (m), hour (h), day of month (dom), month (mon),
10 # and day of week (dow) or use '*' in these fields (for 'any').# 
11 # Notice that tasks will be started based on the cron's system
12 # daemon's notion of time and timezones.
13 # 
14 # Output of the crontab jobs (including errors) is sent through
15 # email to the user the crontab file belongs to (unless redirected).
16 # 
17 # For example, you can run a backup of all your user accounts
18 # at 5 a.m every week with:
19 # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
20 # 
21 # For more information see the manual pages of crontab(5) and cron(8)
22 # 
23 # m h  dom mon dow   command
24 37 0 * * * echo hell 
25 root@xxj-VirtualBox:~# crontab -e

[root@www ~]# crontab [-u username] [-l|-e|-r]
选项与参数:
-u :只有 root 才能进行这个任务,亦即帮其他使用者建立/移除 crontab 工作排程;
-e :编辑 crontab 的工作内容
-l :查阅 crontab 的工作内容
-r :移除所有的 crontab 的工作内容,若仅要移除一项,请用 -e 去编辑。

范例一:用 dmtsai 的身份在每天的 12:00 发信给自己
[dmtsai@www ~]$ crontab -e
# 此时会进入 vi 的编辑画面让您编辑工作!注意到,每项工作都是一行。
0 12 * * * mail dmtsai -s "at 12:00" < /home/dmtsai/.bashrc
#分 时 日 月 周 |<==============指令串========================>|

代表意义 分钟 小时 日期 月份 周 指令
数字范围 0-59 0-23 1-31 1-12 0-7

特殊字符

代表意义

*(星号)

代表任何时刻都接受的意思!举例来说,范例一内那个日、月、周都是 , 就代表着『不论何月、何日的礼拜几的 12:00 都执行后续指令』的意思!

,(逗号)

代表分隔时段的意思。举例来说,如果要下达的工作是 3:00 与 6:00 时,就会是:

0 3,6 * * * command

时间参数还是有五栏,不过第二栏是 3,6 ,代表 与 都适用!

-(减号)

代表一段时间范围内,举例来说, 点到 12 点之间的每小时的 20 分都进行一项工作:

20 8-12 * * * command

仔细看到第二栏变成 8-12 喔!代表 8,9,10,11,12 都适用的意思!

/n(斜线)

那个 代表数字,亦即是『每隔 单位间隔』的意思,例如每五分钟进行一次,则:

*/5 * * * * command

很简单吧!用 与 /5 来搭配,也可以写成 0-59/5 ,相同意思!

原文地址:https://www.cnblogs.com/linyx/p/4014813.html