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

四、用法举例

1.建立演示账号crontab。

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

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

[root@GeekDevOps ~]# date
2018年 03月 18日 星期日 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

例子中表示每隔2分钟执行一次命令: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 3月  19 01:15 /home/crontab/GeekDevOps.txt
[root@GeekDevOps ~]# cat /home/crontab/GeekDevOps.txt 
GeekDevOps

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

相关文章
|
2月前
|
Ubuntu Linux
计算机基础知识:linux系统怎么安装?
在虚拟机软件中创建一个新的虚拟机,并选择相应操作系统类型和硬盘空间大小等参数。将下载的 ISO 镜像文件加载到虚拟机中。启动虚拟机,进入安装界面,并按照步骤进行安装。安装完成后,可以在虚拟机中使用 Linux 系统。
|
2月前
|
缓存 监控 Linux
Linux系统清理缓存(buff/cache)的有效方法。
总结而言,在大多数情形下你不必担心Linux中buffer与cache占用过多内存在影响到其他程序运行;因为当程序请求更多内存在没有足够可用资源时,Linux会自行调整其占有量。只有当你明确知道当前环境与需求并希望立即回收这部分资源给即将运行重负载任务之前才考虑上述方法去主动干预。
796 10
|
2月前
|
安全 Linux 数据安全/隐私保护
为Linux系统的普通账户授予sudo访问权限的过程
完成上述步骤后,你提升的用户就能够使用 `sudo`命令来执行管理员级别的操作,而无需切换到root用户。这是一种更加安全和便捷的权限管理方式,因为它能够留下完整的权限使用记录,并以最小权限的方式工作。需要注意的是,随意授予sudo权限可能会使系统暴露在风险之中,尤其是在用户不了解其所执行命令可能带来的后果的情况下。所以在配置sudo权限时,必须谨慎行事。
335 0
|
2月前
|
Ubuntu Linux 开发者
国产 Linux 发行版再添新成员,CutefishOS 系统简单体验
当然,系统生态构建过程并不简单,不过为了帮助国产操作系统优化生态圈,部分企业也开始用国产操作系统替代 Windows,我们相信肯定会有越来越多的精品软件登录 Linux 平台。
122 0
|
2月前
|
Ubuntu 安全 Linux
Linux系统入门指南:从零开始学习Linux
Shell脚本是一种强大的自动化工具,可以帮助您简化重复的任务或创建复杂的脚本程序。了解Shell脚本的基本语法和常用命令,以及编写和运行Shell脚本的步骤,将使您更高效地处理日常任务。
220 0
|
2月前
|
Ubuntu Linux 图形学
Linux学习之Linux桌面系统有哪些?
Cinnamon:与MATE类似,Cinnamon 拥有 GNOME 和 Unity 等其它桌面环境所没有的种种功能,是高度可定制的桌面环境,不需要任何外部插件、窗口组件和调整工具来定制桌面。
128 0
|
2月前
|
Ubuntu 安全 Linux
十款常用Linux系统介绍
本文不是什么大盘点。市面上有好几百款发行版,每款发行版在某个方面都与众不同。不可能在此全部罗列,本文只罗列了十款最常见的Linux发行版(世界上只有两种人,一种是懂二进制的,另一种是不懂二进制的)。请宣传Linux的魅力或威力。
|
Unix Linux
Linux中的Crontab:定时任务管理器
`crontab`是Linux下的定时任务管理器,用于设置周期性执行的任务。用户可以通过`crontab -l`查看任务,`crontab -e`编辑,`crontab -r`删除任务。任务格式为:`* * * * * command`,分别代表分钟、小时、日期、月份、星期,例如`30 10 * * * /path/to/script.sh`。注意确保命令有执行权限,处理环境变量,并关注日志文件 `/var/log/syslog` 或 `/var/log/cron`。学会使用`crontab`能有效自动化Linux系统的日常任务。
|
12月前
|
Linux
Linux Crontab 查看定时任务启动没
【10月更文挑战第20天】在Linux系统中,crontab用于设置周期性执行的任务。查看当前用户的Crontab任务列表,使用`crontab -l`;查看所有用户任务,使用`sudo crontab -l`或指定用户`sudo crontab -u username -l`。
553 5
|
数据挖掘 Linux Shell
linux 使用crontab 创建定时任务
linux 使用crontab 创建定时任务
251 0
linux 使用crontab 创建定时任务