当前位置: 首页 > 编程文章 >

Linux crontab定时任务配置方法(详解)

Linux crontab定时任务配置方法(详解)

作者:许宁时间:2025-06-18 10:49:09浏览:623

本文详细介绍Linux系统中crontab定时任务的配置方法。cron作为系统主要的调度进程,允许用户在无需人工干预的情况下运行作业。文章涵盖crontab文件的存储位置、权限控制文件的作用、基本操作命令以及时间表达式的详细语法,并针对环境变量和路径设置等常见注意事项进行说明,帮助用户正确配置和管理后台自动化任务。

本文目录展开

Linux crontab定时任务配置方法(详解)

CRONTAB概念/介绍

crontab命令主要用于设定需要周期性执行的指令。该命令通过标准输入设备读取指令,并将其保存至“crontab”文件中,以便后续读取和执行。

cron是系统的核心调度进程。它能够在每天的非高峰时段或一周、一月中的不同时间段自动执行作业,无需人工干预。用户可以使用crontab命令来提交、编辑或删除相应的作业。每个用户都可以拥有一个独立的crontab文件来保存各自的调度信息。系统管理员则可以通过cron.deny和cron.allow这两个文件来控制哪些用户被禁止或允许拥有自己的crontab文件。

1:crontab文件通常位于/etc/目录下,这里存放着系统运行的调度程序。

[root@localhost cron]# more /etc/crontab

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

HOME=/

# run-parts

01 * * * * root run-parts /etc/cron.hourly

02 4 * * * root run-parts /etc/cron.daily

22 4 * * 0 root run-parts /etc/cron.weekly

42 4 1 * * root run-parts /etc/cron.monthly

2:每个用户都会自动生成一个属于自己的crontab文件,一般位于/var/spool/cron目录下。

[root@localhost cron]# cd /var/spool/cron

[root@localhost cron]# ls

oracle root

如果执行命令crontab -r,将会删除当前用户的crontab文件。例如切换到oracle账号下执行该命令后,/var/spool/cron/oracle文件就会被删除。若要重新创建该文件,只需使用crontab -e命令即可。需要注意的是,普通用户通常没有权限直接访问/var/spool/cron目录。

3:cron.deny 与 cron.allow文件

/etc/cron.deny 表示不能使用crontab 命令的用户

/etc/cron.allow 表示能使用crontab的用户。

默认情况下,cron.allow文件不存在。如果两个文件同时存在,那么/etc/cron.allow 优先。如果两个文件都不存在,那么只有超级用户可以安排作业。

但是让人纳闷的是cron.deny文件是空的,如下所示

[oracle@localhost etc]$ more cron.deny

4:cron.hourly,cron.daily,cron.monthly,cron.weekly

[oracle@localhost etc]$ ls -lrt cron*

-rw-r--r-- 1 root root 255 Sep 21 2004 crontab

-rw-r--r-- 1 root root 0 May 16 2008 cron.deny

cron.hourly:

total 0

cron.monthly:

total 8

-rwxr-xr-x 1 root root 278 Sep 29 2004 0anacron

cron.weekly:

total 16

-rwxr-xr-x 1 root root 277 Sep 29 2004 0anacron

-rwxr-xr-x 1 root root 414 Jun 6 2007 00-makewhatis.cron

cron.daily:

total 60

-rwxr-xr-x 1 root root 286 Aug 14 2004 tmpwatch

-rwxr-xr-x 1 root root 276 Sep 29 2004 0anacron

-rwxr-xr-x 1 root root 2133 Dec 1 2004 prelink

-rwxr-xr-x 1 root root 180 May 2 2006 logrotate

-rwxr-xr-x 1 root root 418 Jun 6 2007 00-makewhatis.cron

-rwxr-xr-x 1 root root 104 Aug 2 2007 rpm

-rwxr-xr-x 1 root root 121 Aug 22 2007 slocate.cron

lrwxrwxrwx 1 root root 28 May 16 2008 00-logwatch -> ../log.d/scripts/logwatch.pl

cron.d:

total 12

-rw------- 1 root root 366 Jun 12 2007 sa-update

-rw-r--r-- 1 root root 188 Jul 18 2007 sysstat

CRONTAB在线手册

注意:不同版本的Linux系统,可能crontab手册内容有所出入,请以实际版本为准。

[oracle@localhost ~]$ man crontab | more


CRONTAB(1)                             CRONTAB(1)



NAME
    crontab - maintain crontab files for individual users (ISC Cron V4.1)

SYNOPSIS
    crontab [-u user] file
    crontab [-u user] [-l | -r | -e] [-i] [-s]

DESCRIPTION
    Crontab is the program used to install, deinstall or list the tables
    used to drive the cron(8) daemon in ISC Cron. 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 can be even
    more crontabs - for each range. For more see selinux(8).

    If the cron.allow file exists, then you must be listed therein in order
    to be allowed to use this command. If the cron.allow file does not
    exist but the cron.deny file does exist, then you must not be listed in
    the cron.deny file in order to use this command. If neither of these
    files exists, only the super user will be allowed to use this command.


OPTIONS
    -u   It specifies the name of the user whose crontab is to be
       tweaked.  If this option is not given, crontab examines "your"
       crontab, i.e., the crontab of the person executing the command.
       Note that su(8) can confuse crontab and that if you are running
       inside of su(8) you should always use the -u option for safety's
       sake.  The first form of this command is used to install a new
       crontab from some named file or standard input if the pseudo-
       filename "-" is given.

    -l   The current crontab will be displayed on standard output.

    -r   The current crontab will be removed.

    -e   This option is used to edit the current crontab using the editor
       specified by the VISUAL or EDITOR environment variables.  After
       you exit from the editor, the modified crontab will be installed
       automatically.

    -i   This option modifies the -r option to prompt the user for a
       'y/Y' response before actually removing the crontab.

    -s   It will append the current SELinux security context string as an
       MLS_LEVEL setting to the crontab file before editing / replace-
       ment occurs - see the documentation of MLS_LEVEL in crontab(5).

SEE ALSO
    crontab(5), cron(8)

FILES
    /etc/cron.allow
    /etc/cron.deny

STANDARDS
    The crontab command conforms to IEEE Std1003.2-1992 (‘‘POSIX'').  This
    new command syntax differs from previous versions of Vixie Cron, as
    well as from the classic SVR3 syntax.

DIAGNOSTICS
    A fairly informative usage message appears if you run it with a bad
    command line.

AUTHOR
    Paul Vixie 



4th Berkeley Distribution    16 Januar 2007           CRONTAB(1)

CRONTAB语法及应用

1:查看当前用户的定时任务

[oracle@localhost ~]$ crontab -l

* * * * * /home/oracle/test.sh >/dev/null 2>&1

2:编辑当前用户的定时任务

可以在编辑状态修改、删除、新增一些定时任务。注释一般用#

[oracle@localhost ~]$ crontab -e

3:删除当前用户的定时任务

[root@localhost ~]# crontab -r

[root@localhost ~]# crontab -l

no crontab for root

4:

如下所示,一般crontab文件里面的定时任务格式如下所示:

59 23 * * * /home/oracle/scripts/alert_log_archive.sh >/dev/null 2>&1

crontab 文件中每个条目中各个域的意义和格式:

第一列 分钟: 1——59

第二列 小时: 1——23(0表示子夜)

第三列 日 : 1——31

第四列 月 : 1——12

第五列 星期: 星期0——6(0表示星期天,1表示星期一、以此类推)

第六列 要运行的命令

我们暂且用C1、C2、C3、C4、C5、C6代表这六列,前面五列通过组合方式来决定执行脚本的频率,最小频率为每分钟执行一次,其中Cn可以用 * ; */n ; T1-T2; a,b,c; 四种形式来表示:

当 C1 为 * 时表示每分钟都要执行脚本,C2 为 * 时表示每小时都要执行程式,依次类推.....

当 C1 为 T1-T2 时表示从第 T1 分钟到第 T2 分钟这段时间内要执行,C2 为 T1-T2 时表示从第 T1 到第 T2 小时都要执行,依次类推....

当 C1 为 */n 时表示每 n 分钟的时间间隔执行一次,C2 为 */n 表示每隔n小时的间隔执行一次,依次类推.....

当 C1 为 a, b, c,... 时表示第 a, b, c,... 分钟要执行,C2 为 a, b, c,... 时表示第 a, b, c...个小时要执行,依次类推....

下面列举几个例子供大家参考

1: 59 23 * * * /home/oracle/scripts/alert_log_archive.sh >/dev/null 2>&1

表示每天23点59分执行脚本/home/oracle/scripts/alert_log_archive.sh

2: */5 * * * * /home/oracle/scripts/monitoring_alert_log.sh >/dev/null 2>&1

表示每5分钟执行一次脚本/home/oracle/scripts/monitoring_alert_log.sh

3: 0 20 * * 1-5 mail -s "**********" kerry@domain.name < /tmp/maildata

周一到周五每天下午 20:00 寄一封信给 kerry@domain.name

..............................................

关于 >/dev/null 2>&1 的解释:

0表示键盘输入

1表示标准输出

2表示错误输出.

我们首先创建test.sh脚本如下:

#! /bin/sh

echo "hello, everybody, now is " `date`

date >> test.txt

然后添加作业

* * * * * /home/oracle/test.sh >/home/oracle/log.txt & 默认值为1,即和下面命令一致

* * * * * /home/oracle/test.sh 1>/home/oracle/log.txt &

* * * * * /home/oracle/test.sh 2>/home/oracle/log.txt &

* * * * * /home/oracle/test.sh 2>/home/oracle/log.txt 2>&1 &

1,2将tesh.sh 命令输出重定向到log.txt, 即输出内容不打印到屏幕上,而是输出到log.txt文件中。如果你需要追加而不是覆盖,可以用 >>代替>

2>&1 是将错误输出重定向到标准输出。 然后将标准输入重定向到文件log.txt。

&1 表示的是文件描述1,表示标准输出,如果这里少了&就成了数字1,就表示重定向到文件1。

注意事项:

配置定时任务时,需要注意两个问题:

1: 在SHELL中设置了必要的环境变量;例如一个shell脚本手工执行OK,但是配置成后台作业执行时,获取不到ORACLE的环境变量,这是因为crontab环境变量问题,Crontab的环境默认情况下并不包含系统中当前用户的环境。所以,你需要在shell脚本中添加必要的环境变量的设置

2: 尽量所有的文件都采用完全路径方式,避免使用相对路径。

以上这篇Linux crontab定时任务配置方法(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持本站。

上一篇
Linux命令行模糊查找文件:find与grep组合实战

在Linux系统中,利用find和grep命令的组合可以高效实现文件的模糊查找。通过-name或-iname参数可按文件名后缀及大小写进行匹配;结合xargs管道可将find结果传递给grep进行内容检索,支持大小写敏感或忽略选项。此外,find命令还能基于访问时间、修改时间、文件大小、所属用户及文件新旧关系等多维度条件精准定位目标文件,满足复杂的运维排查需求。

下一篇
Navicat连接Linux MySQL失败排查与解决

本文介绍解决Navicat无法连接Linux服务器上MySQL数据库的方法。主要涉及开放3306端口、修改iptables规则、重启防火墙服务,以及配置MySQL允许root远程登录和监听所有IP地址。通过调整系统防火墙和MySQL配置文件中的bind-address参数,可恢复远程连接。

相关文章

更多
Linux配置定时任务:crontab -e与编辑/etc/crontab的区别

在Linux系统中配置定时任务时,通常有两种主要方式:使用 `crontab -e` 命令和直接编辑 `/etc/crontab` 文件。前者属于用户级配置,脚本会被写入 `/var/spool/cron/` 目录下以用户名命名的文件中,且系统会协助检查语法错误;后者属于系统级配置,适合需要指定执行用户的场景,但需手动维护格式。本文详细解析这两种方法的具体差异及适用场景。

2025.03.28 729
Linux crontab定时任务配置与权限管理详解

本文详解Linux系统中由cron守护进程管理的定时任务机制。重点介绍crontab配置文件的三个查找位置(/var/spool/cron、/etc/crontab、/etc/cron.d/),阐述cron.allow与cron.deny的权限控制逻辑,并解析时间字段语法及run-parts参数在系统维护脚本中的应用。

2025.02.05 722
Linux设置定时任务的方法步骤

在Linux系统中,通过crontab命令可以高效地设置和管理定时任务。本文详细讲解了从登录、查看到编辑任务的完整流程,重点解析了分钟、小时等五个字段的时间格式及星号、逗号等特殊字符的含义,并提供了多个具体执行案例及重启服务的操作说明。

2026.02.02 591
Linux crontab定时任务配置与使用指南

crontab是Unix和类Unix系统中用于设置周期性执行指令的工具,其配置文件由用户命名并存储在/var/spool/cron/目录下。该机制依赖后台守护进程crond每分钟检查一次预定作业。本文详细解析crontab的六字段时间格式、常用管理命令、基于allow/deny文件的权限控制逻辑,以及如何在Linux中通过命令行调用PHP脚本实现自动化任务。

2026.07.22 511
Linux crontab 定时任务配置详解:从语法到实战示例

本文介绍 Linux 系统中 crontab 命令的语法格式及常见定时任务的配置方法。通过具体示例,演示如何设置每分钟、每小时、每天、每周及每月的自动执行计划,涵盖添加、查询任务的基本操作及复杂时间规则的应用。

2026.07.31 374
Linux Crontab定时任务配置与使用指南

Crontab是Linux系统中用于周期性执行任务的核心工具,由后台守护进程crond每分钟检查并触发。其配置文件遵循minute hour day month week command的五段式时间格式,支持星号、逗号、中杠及正斜线等通配符以定义复杂调度规则。通过创建可执行脚本并编辑/etc/crontab或用户级crontab文件,结合systemctl管理服务状态,即可实现自动化运维操作。

2026.08.02 157

专题推荐

更多
PHP 类与对象定义及继承

先看类声明、对象实例与属性访问。理解对象的创建和销毁后,再读继承、方法重载和关键字对照。已有代码遇到继承行为差异时,可进一步查看 final 方法与反射实例。

PHP 定时任务配置与运行

先确定任务运行间隔以及是否允许重叠。固定时间触发先看 Cron 案例,框架内和常驻服务再读对应示例。

Yii2 队列配置与常驻运行

先用一个最小任务跑通入队和消费,再检查 Web 与 Console 是否加载了相同的队列配置。确认命令能手动运行后再配置 Supervisor,最后验证进程重启时的处理情况。

PHP Session 过期时间设置方法

浏览器关闭、服务端回收和业务规定的闲置超时,可能在不同时间发生。对照配置项与应用层时间戳的做法,检查会话何时仍可读、何时应要求重新登录。

PHP URL 编码和解码方法

URL 路径片段和查询参数对空格的处理方式不同。先比较 urlencode 与 rawurlencode,再沿解码过程核对中文、加号和百分号,检查地址是否被重复编码或提前解码。

在线工具箱

更多
Cron 执行时间预览
Cron 执行时间预览

检查五段 Cron 表达式,在指定时区预览未来执行时刻,支持范围、列表与步长,处理夏令时跳时和重复时刻。

键码查询
键码查询

按下键盘按键时查看浏览器记录的键名和事件信息。

.env 配置检查
.env 配置检查

本地检查 .env 键值语法、引号、重复键及空值,预览解析结果,辅助排查 PHP 和 Laravel 环境配置。

进制转换
进制转换

转换二进制、十进制、十六进制等数值表示,辅助程序调试与进制学习。

HTML 标签安全清理
HTML 标签安全清理

清理 HTML 片段中的标签或危险内容,辅助整理待展示的文本。