Linux下定时任务(系统任务调度、用户任务调度)crontab使用详解

一、简介

crond是Linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,在CentOS Linux release 7.2.1511中默认是开机启动的,大家可以使用命令:systemctl status crond进行查看。 crond进程定期(每分钟)检查是否有要执行的任务,如果有要执行的任务,则自动执行该任务。用户在cron表
(也被称为crontab文件)指定了定时任务,crontab也就是我们常见的定时任务设置命令。Linux下的任务调度分为两类,系统任务调度和用户任务调度。

系统任务调度:系统周期性所要执行的工作,比如写缓存数据到硬盘、日志清理等。/etc/crontab文件就是系统任务调度的配置文件。

[root@GeekDevOps ~]# cat /etc/crontab 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# 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
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

用户任务调度:用户定期要执行的工作,比如用户数据备份、定时邮件提醒等。用户可以使用 crontab 工具来定制自己的计划任务。所有用户定义的crontab文件都被保存在/var/spool/cron目录中。其文件名与用户名一致,使用者权限文件如下:

/etc/cron.deny 该文件中所列用户不允许使用crontab命令

/etc/cron.allow 该文件中所列用户允许使用crontab命令,在CentOS7.2中不存在该文件(If the cron.allow file exists, a user must be listed in it to be allowed to use cron If the cron.allow file does not exist but the cron.deny file does exist, then a user must not be listed in the cron.deny file in order to use cron. If neither of these files exists, only the super user is allowed to use cron.)

/var/spool/cron/ 所有用户crontab文件存放的目录,以用户名命名。

二、通过官方渠道获取帮助资料

  • crond
[root@GeekDevOps ~]# crond -h
Usage:
 crond [options]
Options:
 -h         print this message 
 -i         deamon runs without inotify support
 -m <comm>  off, or specify prefered client for sending mails
 -n         run in foreground
 -p         permit any crontab
 -P         use PATH="/usr/bin:/bin"
 -c         enable clustering support
 -s         log into syslog instead of sending mails
 -x <flag>  print debug information
[root@GeekDevOps ~]# man crond
CRON(8)                                                                           System Administration                                                                          CRON(8)
NAME
       crond - daemon to execute scheduled commands

SYNOPSIS
       crond [-c | -h | -i | -n | -p | -P | -s | -m<mailcommand>]
       crond -x [ext,sch,proc,pars,load,misc,test,bit]

通过以上帮助信息,我们可以知道crond是执行任务计划的一个守护进程。在使用crontab之前我们可以根据帮助信息来设置相关选项,一般情况下我们都使用默认值。

  • crontab
[root@GeekDevOps ~]# crontab --help
crontab:无效选项 -- -
crontab: usage error: unrecognized option
Usage:
 crontab [options] file
 crontab [options]
 crontab -n [hostname]
Options:
 -u <user>  define user
 -e         edit user's crontab
 -l         list user's crontab
 -r         delete user's crontab
 -i         prompt before deleting
 -n <host>  set host in cluster to run users' crontabs
 -c         get host in cluster to run users' crontabs
 -s         selinux context
 -x <mask>  enable debugging
 CRONTAB(1)                                                                            User Commands                                                                           CRONTAB(1)
[root@GeekDevOps ~]# man crontab
NAME
       crontab - maintains crontab files for individual users
SYNOPSIS
       crontab [-u user] file
       crontab [-u user] [-l | -r | -e] [-i] [-s]
       crontab -n [ hostname ]
       crontab -c
DESCRIPTION
       Crontab  is  the  program  used  to install a crontab table file, remove or list the existing tables used to serve the cron(8) daemon.  Each user can have their own crontab, and
       though these are files in /var/spool/, they are not intended to be edited directly.  For SELinux in MLS mode, you can define more crontabs for each range.  For more information,
       see selinux(8).
       In  this  version  of Cron it is possible to use a network-mounted shared /var/spool/cron across a cluster of hosts and specify that only one of the hosts should run the crontab
       jobs in the particular directory at any one time.  You may also use crontab(1) from any of these hosts to edit the same shared set of crontab files, and to set and  query  which
       host should run the crontab jobs.

三、特别说明

1.通过/etc/crontab文件,我们可以清楚地看到每个字段的含义及取值范围,此处不再进行赘述。
2.特殊字符的用法

特殊字符 含义
*(星号) 代表所有可能的值,只要其他值满足都执行
,(逗号) 用逗号隔开表示该字段取值
-(减号) 两个可取值的整数之前的取值范围
/n(斜杠) 时间间隔频率

3.系统任务调度我们可以通过/etc/crontab文件直接配置。用户任务调度我们一般通过crontab命令来进行配置,用户任务调度的配置保存/var/spool/cron/目录下,并以用户名称命名。系统任务调度可以通过直接修改/etc/crontab来配置。新创建的cron job,不会马上执行,至少要过2分钟才执行。如果重启crond则马上执行。
4.当crontab突然失效时,可以尝试/etc/init.d/crond restart解决问题。或者查看日志看某个job有没有执行/报错tail -f /var/log/cron。
5.运行crontab -r需要特别谨慎。它从Crontab目录(/var/spool/cron)中删除用户的crontab文件。删除了该用户的所有crontab都没了。
6.在crontab中%是有特殊含义的,表示换行的意思。如果要用的话必须进行转义\%,如经常用的date ‘+%Y%m%d’在crontab里是不会执行的,应该换成date ‘+\%Y\%m\%d’。

四、用法举例

1.建立演示账号crontab。

[root@GeekDevOps ~]# useradd -c "Crontab demo." crontab

2.星号(*)使用举例。

[root@GeekDevOps ~]# date
20180318日 星期日 23:58:50 CST
[root@GeekDevOps ~]# crontab -u crontab -e
no crontab for crontab - using an empty one
crontab: installing new crontab
[root@GeekDevOps ~]# crontab -l -u crontab
5 0 * * *  echo "GeekDevOps"
[root@GeekDevOps ~]# su crontab
[crontab@GeekDevOps ~]$ cat /var/spool/mail/crontab
From crontab@GeekDevOps.localdomain  Mon Mar 19 00:05:01 2018
Return-Path: <crontab@GeekDevOps.localdomain>
X-Original-To: crontab
Delivered-To: crontab@GeekDevOps.localdomain
Received: by GeekDevOps.localdomain (Postfix, from userid 1000)
    id 4B1648D618; Mon, 19 Mar 2018 00:05:01 +0800 (CST)
From: "(Cron Daemon)" <crontab@GeekDevOps.localdomain>
To: crontab@GeekDevOps.localdomain
Subject: Cron <crontab@GeekDevOps> echo "GeekDevOps"
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=52>
X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/1000>
X-Cron-Env: <LANG=zh_CN.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/crontab>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=crontab>
X-Cron-Env: <USER=crontab>
Message-Id: <20180318160501.4B1648D618@GeekDevOps.localdomain>
Date: Mon, 19 Mar 2018 00:05:01 +0800 (CST)
GeekDevOps

以上例子中完整演示了crontab从建立到执行的过程。“5 0 * * * echo “GeekDevOps””表示在每天00:05执行命令:echo “GeekDevOps”。后面的星号表示只要前面条件满足都执行。例子中的-u选项指定了用户:crontab,-l选项列举了相关用户的用户任务调度,不指定用户则默认为root。执行结果默认写入到用户mail目录下的相关文件中。

3.逗号(,)的使用举例。

[crontab@GeekDevOps ~]$ crontab -e
crontab: installing new crontab
[crontab@GeekDevOps ~]$ crontab -l
5 0 * * *  echo "GeekDevOps"
3 2,6,8 * * * ls /usr/local

现在我们已经把用户切换到crontab下,因此无需额外指定-u选项相关内容。“3 2,6,8 * * *”表示每天的02:03:00、06:03:00、08:03:00分别执行一次命令:ls /usr/local。

4.减号(-)的使用举例。

[crontab@GeekDevOps ~]$ crontab -e
crontab: installing new crontab
[crontab@GeekDevOps ~]$ crontab -l
5 0 * * *  echo "GeekDevOps"
3 2,6,8 * * * ls /usr/local
0 2-6 * * 6 df -h /

例子中的“0 2-6 * * 6 df -h /”表示用户crontab在每周六的02:00、03:00、04:00、05:00、06:00执行命令:df -h / 。

5.斜杠(/)的使用举例。

[crontab@GeekDevOps ~]$ exit
exit
[root@GeekDevOps ~]# crontab -e 
no crontab for root - using an empty one
crontab: installing new crontab
[root@GeekDevOps ~]# crontab -l
*/1 * * * * echo "GeekDevOps">>/root/GeekDevOps.txt
[root@GeekDevOps ~]# cat /root/GeekDevOps.txt 
GeekDevOps
GeekDevOps

例子中表示每隔1分钟执行一次命令:echo “GeekDevOps”>>/root/GeekDevOps.txt。

6.crontab的使用非常简单,很容易理解,只要在取值范围内设置执行的值基本是没有问题的。现在我们要删除已经设置的这些定时任务。

[root@GeekDevOps ~]# crontab -u crontab -r -i
crontab: really delete crontab's crontab? y
[root@GeekDevOps ~]# crontab -u crontab -l
no crontab for crontab

例子中,选项-r表示删除所有定时任务。选项-i表示在删除前进行再次确定,输入y或者Y才能真正删除。

7.备份我们设置的用户任务调度配置文件。

[root@GeekDevOps ~]# cat /var/spool/cron/root 
*/1 * * * * echo "GeekDevOps">>/root/GeekDevOps.txt
[root@GeekDevOps ~]# cat /var/spool/cron/root >>contab.bak

8.系统任务调度的使用举例。

[root@GeekDevOps ~]# vi /etc/crontab 
[root@GeekDevOps ~]# cat /etc/crontab 
*/1 * * * * crontab echo "GeekDevOps">>~/GeekDevOps.txt
[root@GeekDevOps ~]# crontab -l
no crontab for root
[root@GeekDevOps ~]# ls -l /home/crontab/GeekDevOps.txt 
-rw-r--r--. 1 crontab crontab 44 319 01:15 /home/crontab/GeekDevOps.txt
[root@GeekDevOps ~]# cat /home/crontab/GeekDevOps.txt 
GeekDevOps

系统任务调度与用户任务调度不一样,需要直接在/etc/crontab里面配置,如果需要指定用户,还需要在执行命令前指定用户名。通过crontab -l 命令是查看不到系统任务调度任务的。

猜你喜欢

转载自blog.csdn.net/solaraceboy/article/details/79606545
今日推荐