crontab

Linux crontab 命令

Linux crontab 是用来定期执行程序的命令。

crond 命令每分钟会定期检查是否有要执行的工作,如果有要执行的工作便会自动执行该工作。

语法

crontab [ -u user ] file

crontab [ -u user ] { -l | -r | -e }

说明:

crontab 是用来让使用者在固定时间或固定间隔执行程序之用,换句话说,也就是类似使用者的时程表。

-u user 是指设定指定 user 的时程表,这个前提是你必须要有其权限(比如说是 root)才能够指定他人的时程表。如果不使用 -u user 的话,就是表示设定自己的时程表。

参数说明:

  • -e : 执行文字编辑器来设定时程表,内定的文字编辑器是 VI,如果你想用别的文字编辑器,则请先设定 VISUAL 环境变数来指定使用那个文字编辑器(比如说 setenv VISUAL joe)
  • -r : 删除目前的时程表
  • -l : 列出目前的时程表

时程表的格式如下:

f1 f2 f3 f4 f5 program

  • 其中 f1 是表示分钟,f2 表示小时,f3 表示一个月份中的第几日,f4 表示月份,f5 表示一个星期中的第几天。program 表示要执行的程序。
  • 当 f1 为 * 时表示每分钟都要执行 program,f2 为 * 时表示每小时都要执行程序,其馀类推
  • 当 f1 为 a-b 时表示从第 a 分钟到第 b 分钟这段时间内要执行,f2 为 a-b 时表示从第 a 到第 b 小时都要执行,其馀类推
  • 当 f1 为 */n 时表示每 n 分钟个时间间隔执行一次,f2 为 */n 表示每 n 小时个时间间隔执行一次,其馀类推
  • 当 f1 为 a, b, c,… 时表示第 a, b, c,… 分钟要执行,f2 为 a, b, c,… 时表示第 a, b, c…个小时要执行,其馀类推

使用者也可以将所有的设定先存放在文件中,用 crontab file 的方式来设定时程表。

1
2
3
4
5
6
7
8
* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
  • “*” 代表取值范围内的数字,

  • “/“ 代表”每”,

  • “-“ 代表从某个数字到某个数字,

  • “,” 分开几个离散的数字

例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 意思是每两个小时重启一次apache 
0 */2 * * * /sbin/service httpd restart

# 意思是每天7:50开启ssh服务
50 7 * * * /sbin/service sshd start

# 意思是每天22:50关闭ssh服务
50 22 * * * /sbin/service sshd stop

# 每月1号和15号检查/home 磁盘
0 0 1,15 * * fsck /home

# 每小时的第一分执行 /home/bruce/backup这个文件
1 * * * * /home/bruce/backup

# 每周一至周五3点钟,在目录/home中,查找文件名为*.xxx的文件,并删除4天前的文件。
00 03 * * 1-5 find /home "*.xxx" -mtime +4 -exec rm {} \;

# 意思是每月的1、11、21、31日是的6:30执行一次ls命令
30 6 */10 * * ls

# 使用 python3.7 执行 /home/ubuntu/PyCharm/EMailBot/bus.py 文件
*/10 * * * * python3.7 /home/ubuntu/PyCharm/EMailBot/bus.py

完整例子

添加计划任务

  1. 执行命令

crontab -e

  1. 使用 vim 编辑文件,在最后一行添加命令

*/10 * * * * python3.7 /home/ubuntu/PyCharm/EMailBot/bus.py 表示每十分钟执行一次 python3.7 /home/ubuntu/PyCharm/EMailBot/bus.py

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

  2. 重启 cron 服务

service cron restart