天天看点

Linux定时任务:Crontab

Linux定时任务:Crontab

在linux系统上,可以使用“crontab”命令创建或删除定时任务,用来周期的执行需要的shell命令或者脚本。

命令:

crontab [-u username] -e/-l/-r

参数:

-u:指定用户

-e:创建任务

-l:列出现有任务

-r:删除任务

crontab可以创建多条任务,每个任务的构成为“时间 + 命令”,每行一个任务。

时间:分、时、日、月、周五种,最小间隔是1分钟,操作符有

  • *:表示所有单位时间
  • /:表示间隔多少单位时间
  • -:表示一个范围的单位时间
  • ,:表示具体的几个单位时间

命令:具体要执行的shell命令或者脚本

示例,新建几个任务:

crontab -e

其实就是编辑下面一个文件,在末尾添加任务即可,为了验证方便,设置的都是分钟:

# 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
* * * * * date >> ~/time.log
*/1 * * * * date >> ~/time1.log
*/2 * * * * date >> ~/time2.log
20-30 * * * * date >> ~/time3.log
20-30/2 * * * * date >> ~/time4.log
20 * * * * date >> ~/time5.log
20,30 * * * * date >> ~/time6.log
           

保存之后,提示:crontab: installing new crontab ,接下来就开始等待执行了,可以用 “crontab -l” 查看刚才添加的任务。

接下来等待一会儿之后,查看执行结果:

* * * * * date >> ~/time.log
#可以看到每分钟都执行了任务
~$ cat time.log 
2021年 08月 09日 星期一 11:19:01 CST
2021年 08月 09日 星期一 11:20:01 CST
2021年 08月 09日 星期一 11:21:01 CST
2021年 08月 09日 星期一 11:22:01 CST
2021年 08月 09日 星期一 11:23:01 CST
2021年 08月 09日 星期一 11:24:01 CST
2021年 08月 09日 星期一 11:25:02 CST
2021年 08月 09日 星期一 11:26:01 CST
2021年 08月 09日 星期一 11:27:01 CST
2021年 08月 09日 星期一 11:28:01 CST
2021年 08月 09日 星期一 11:29:01 CST
2021年 08月 09日 星期一 11:30:01 CST
2021年 08月 09日 星期一 11:31:01 CST
           
*/1 * * * * date >> ~/time1.log
#可以看到,每隔1分钟,执行了任务,和上面每分钟结果一样
~$ cat time1.log 
2021年 08月 09日 星期一 11:19:01 CST
2021年 08月 09日 星期一 11:20:01 CST
2021年 08月 09日 星期一 11:21:01 CST
2021年 08月 09日 星期一 11:22:01 CST
2021年 08月 09日 星期一 11:23:01 CST
2021年 08月 09日 星期一 11:24:01 CST
2021年 08月 09日 星期一 11:25:02 CST
2021年 08月 09日 星期一 11:26:01 CST
2021年 08月 09日 星期一 11:27:01 CST
2021年 08月 09日 星期一 11:28:01 CST
2021年 08月 09日 星期一 11:29:01 CST
2021年 08月 09日 星期一 11:30:01 CST
2021年 08月 09日 星期一 11:31:01 CST
           
*/2 * * * * date >> ~/time2.log
#每隔2分钟执行一次任务
~$ cat time2.log  
2021年 08月 09日 星期一 11:20:01 CST
2021年 08月 09日 星期一 11:22:01 CST
2021年 08月 09日 星期一 11:24:01 CST
2021年 08月 09日 星期一 11:26:01 CST
2021年 08月 09日 星期一 11:28:01 CST
2021年 08月 09日 星期一 11:30:01 CST
           
20-30 * * * * date >> ~/time3.log
#只有20~30分这几分钟,有去执行任务,对比每分钟都执行的命令,少了19分和31分
~$ cat time3.log  
2021年 08月 09日 星期一 11:20:01 CST
2021年 08月 09日 星期一 11:21:01 CST
2021年 08月 09日 星期一 11:22:01 CST
2021年 08月 09日 星期一 11:23:01 CST
2021年 08月 09日 星期一 11:24:01 CST
2021年 08月 09日 星期一 11:25:02 CST
2021年 08月 09日 星期一 11:26:01 CST
2021年 08月 09日 星期一 11:27:01 CST
2021年 08月 09日 星期一 11:28:01 CST
2021年 08月 09日 星期一 11:29:01 CST
2021年 08月 09日 星期一 11:30:01 CST
           
20-30/2 * * * * date >> ~/time4.log
#只有20~30分这几分钟之内,每隔2分钟,执行一次任务
~$ cat time4.log  
2021年 08月 09日 星期一 11:20:01 CST
2021年 08月 09日 星期一 11:22:01 CST
2021年 08月 09日 星期一 11:24:01 CST
2021年 08月 09日 星期一 11:26:01 CST
2021年 08月 09日 星期一 11:28:01 CST
2021年 08月 09日 星期一 11:30:01 CST
           
20 * * * * date >> ~/time5.log
#就20分的时候,执行一次
~$ cat time5.log  
2021年 08月 09日 星期一 11:20:01 CST
           
20,30 * * * * date >> ~/time6.log
#就20分和30分的时候,各执行一次
~$ cat time6.log  
2021年 08月 09日 星期一 11:20:01 CST
2021年 08月 09日 星期一 11:30:01 CST
           

不需要某一条任务的时候,可以使用 “crontab -e” 重新编辑,删除某一条任务即可;如果需要删除用户下所有任务,可以使用 “crontab -r”,执行之后,再去用 “crontab -l” 查看,提示:no crontab for username !

继续阅读