Bootstrap

服务器搭建(07)定时执行脚本

1 安装crontab

执行命令:

$sudo apt-get install cron

crontab命令参数说明:

  • -u user:用来设定某个用户的crontab服务;
  • file:file是命令文件的名字,表示将file做为crontab的任务列表文件并载入crontab。如果在命令行中没有指定这个文件,crontab命令将接受标准输入(键盘)上键入的命令,并将它们载入crontab。
  • -e:编辑某个用户的crontab文件内容。如果不指定用户,则表示编辑当前用户的crontab文件。
  • -l:显示某个用户的crontab文件内容,如果不指定用户,则表示显示当前用户的crontab文件内容。
  • -r:从/var/spool/cron目录中删除某个用户的crontab文件,如果不指定用户,则默认删除当前用户的crontab文件。
  • -i:在删除用户的crontab文件时给确认提示。

2 使用crontab命令来添加脚本

$ crontab -l
no crontab for admin-server
$ crontab -e
no crontab for admin-server - using an empty one

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny
  4. /usr/bin/code
  5. /bin/ed

Choose 1-5 [1]: 3
crontab: installing new crontab

到这里 开始编辑crontab的配置文件:

# 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

这里添加一个新行:

30 20 * * * 脚本绝对路径 #这里表示每天20:30分开始执行一次该脚本

编辑好后:wq保存。之后执行命令查看添加内容,如下所是:

$ crontab -l
# 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
30 20 * * * 脚本绝对路径

每一行有6个参数,这里对时间参数进行下说明:

命令
mhdommondowcommand
  • 第1列表示分钟1~59 每分钟用*或者 */1表示 
  • 第2列表示小时1~23(0表示0点) 
  • 第3列表示日期1~31 
  • 第4列表示月份1~12 
  • 第5列标识号星期0~6(0表示星期天) 
  • 第6列要运行的命令 

同时这里再给出一些其他的案例,如下所示:

30 21 * * * 脚本A
#上面的例子表示每晚的21:30 执行 脚本A。 

45 4 1,10,22 * * 脚本B
#上面的例子表示每月1、10、22日的4 : 45 执行 脚本B。 

10 1 * * 6,0 脚本C
#上面的例子表示每周六、周日的1 : 10 执行 脚本C。

 

;