`
dcj3sjt126com
  • 浏览: 1855862 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Linux Crontab 定时任务 命令详解

阅读更多

.  Crontab 介绍

 

       crontab命令的功能是在一定的时间间隔调度一些命令的执行。

 

1.1 /etc/crontab 文件

       /etc目录下有一个crontab文件,这里存放有系统运行的一些调度程序。每个用户可以建立自己的调度crontab

      

如:

[root@dave ~]# cat /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

 

1.2 /etc/cron.deny  /etc/cron.allow 文件

 

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

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

 

如果两个文件同时存在,那么/etc/cron.allow 优先。

如果两个文件都不存在,那么只有超级用户可以安排作业。

 

 

每个用户都会生成一个自己的crontab 文件。这些文件在/var/spool/cron目录下:

 

如:

[root@dave ~]# cd /var/spool/cron

[root@dave cron]# ls

oracle  root

 

我们直接查看这个文件,里面的内容和对应用户显示的crontab -l 一致。

[root@dave cron]# cat oracle

00 6 * * * /u02/scripts/del_st_archive.sh >/u02/scripts/del_st_arch.log 2>&1

[root@dave cron]# cat root

0 12 * * * /root/bin/sync-clock.sh

[root@dave cron]#

 

 

.  Crontab 使用说明

 

2.1  Crontab语法

usage:  crontab [-u user] file

        crontab [-u user] [ -e | -l | -r ]

                (default operation is replace, per 1003.2)

        -e      (edit user's crontab)

        -l      (list user's crontab)

        -r      (delete user's crontab)

        -i      (prompt before deleting user's crontab)

        -s      (selinux context)

 

       其中,file是命令文件的名字。如果在命令行中指定了这个文件,那么执行crontab命令,则将这个文件拷贝到crontabs目录下;如果在命令行中没有制定这个文件,crontab命令将接受标准输入(键盘)上键入的命令,并将他们也存放在crontab目录下。

 

 

帮助:

[root@dave ~]# man crontab

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 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 edi-tor, 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 / replacement 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 <vixie@isc.org>

4th Berkeley Distribution       16 Januar 2007                      CRONTAB(1)

 

 

 

 

 

2.2  Crontab 格式说明

       我们可以用crontab -e 添加要执行的命令。 命令执行的结果,无论是标准输出还是错误输出,都将以邮件形式发给用户。

      

   添加的命令必须以如下格式:

   * * * * * /command path

      

       前五个字段可以取整数值,指定何时开始工作,第六个域是字符串,即命令字段,其中包括了crontab调度执行的命令。 各个字段之间用spacestabs分割。

 

5个字段分别表示:

       分钟:0-59

       小时:1-23

       日期:1-31

       月份:1-12

       星期:0-60表示周日)

 

还可以用一些特殊符号:

       * 表示任何时刻

       ,: 表示分割

  -:表示一个段,如第二端里: 1-5,就表示15

       /n : 表示每个n的单位执行一次,如第二段里,*/1, 就表示每隔1个小时执行一次命令。也可以写成1-23/1.

 

 

一些示例:

00 8,12,16 * * * /data/app/scripts/monitor/df.sh

30 2 * * * /data/app/scripts/hotbackup/hot_database_backup.sh

10 8,12,16 * * * /data/app/scripts/monitor/check_ind_unusable.sh

10 8,12,16 * * * /data/app/scripts/monitor/check_maxfilesize.sh

10 8,12,16 * * * /data/app/scripts/monitor/check_objectsize.sh

 

43 21 * * * 21:43 执行

15 05 * * *    05:15 执行

0 17 * * * 17:00 执行

0 17 * * 1 每周一的 17:00 执行

0,10 17 * * 0,2,3 每周日,周二,周三的 17:00 17:10 执行

0-10 17 1 * * 毎月1日从 17:007:10 毎隔1分钟 执行

0 0 1,15 * 1 毎月1日和 15日和 一日的 0:00 执行

42 4 1 * *     毎月1日的 4:42 执行

0 21 * * 1-6   周一到周六 21:00 执行

0,10,20,30,40,50 * * * * 每隔10 执行

*/10 * * * *        每隔10 执行

* 1 * * *         1:01:59 每隔1分钟 执行

0 1 * * *         1:00 执行

0 */1 * * *        毎时0 每隔1小时 执行

0 * * * *         毎时0 每隔1小时 执行

2 8-20/3 * * *      8:02,11:02,14:02,17:02,20:02 执行

30 5 1,15 * *       1  15日的 5:30 执行

 

 

2.3  后台执行命令

 

       当在前台运行某个作业时,终端被该作业占据;而在后台运行作业时,它不会占据终端。可以使用&命令把作业放到后台执行。

 

       如:

       30 2 * * * /data/app/scripts/hotbackup/hot_database_backup.sh &

 

       在后台运行作业时要当心:需要用户交互的命令不要放在后台执行,因为这样你的机器就会在那里傻等。

       不过,作业在后台运行一样会将结果输出到屏幕上,干扰你的工作。如果放在后台运行的作业会产生大量的输出,最好使用下面的方法把它的输出重定向到某个文件中:

       如:

              command >out.file 2>&1 &

 

       在这个例子中,2>&1表示所有的标准输出和错误输出都将被重定向到一个叫做out.file 的文件中。

 

2.4  2>&1 含义

 

先看一个例子:

0 2 * * * /u01/test.sh >/dev/null 2>&1 &

 

这句话的意思就是在后台执行这条命令,并将错误输出2重定向到标准输出1,然后将标准输出1全部放到/dev/null 文件,也就是清空。

 

在这里有有几个数字的意思:

       0表示键盘输入

       1表示标准输出

       2表示错误输出.

 

 

我们也可以这样写:

0 2 * * * /u01/test.sh  >/u01/out.file &  --这里没写,默认是1

0 2 * * * /u01/test.sh  1>/u01/out.file &

0 2 * * * /u01/test.sh  2>/u01/out.file &

0 2 * * * /u01/test.sh  2>/u01/out.file  2>&1 &

 

tesh.sh 命令输出重定向到out.file, 即输出内容不打印到屏幕上,而是输出到out.file文件中。

 

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

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

 

:后台执行

 

测试:

ls 2>1  不会报没有2文件的错误,但会输出一个空的文件1

ls xxx 2>1 没有xxx这个文件的错误输出到了1中;

ls xxx 2>&1 不会生成1这个文件了,不过错误跑到标准输出了;

ls xxx >out.txt 2>&1 == ls xxx 1>out.txt 2>&1  因为重定向符号>默认是1,这句就把错误输出和标准输出都传到out.txt 文件中。

 

 

2.5  2>&1写在后面的原因

       格式:command > file 2>&1   == command  1> file 2>&1

 

       首先是command > file将标准输出重定向到file中, 2>&1 是标准错误拷贝了标准输出,也就是同样被重定向到file中,最终结果就是标准输出和错误都被重定向到file中。

 

如果改成: command 2>&1 >file

       2>&1 标准错误拷贝了标准输出的行为,但此时标准输出还是在终端。>file 后输出才被重定向到file,但标准错误仍然保持在终端。

分享到:
评论

相关推荐

    linux crontab定时触发命令.docx

    【Linux crontab定时触发命令详解】 Linux系统中的crontab命令是一个强大的定时任务调度工具,用于设置周期性被执行的任务。通过crontab,我们可以设定特定时间执行指定的命令或者脚本,以实现自动化维护和管理。...

    crontab定时任务服务详解.docx

    ### Crontab定时任务服务详解 #### 一、Crontab简介 Crontab是Linux系统中的一个重要的工具,用于管理周期性执行的任务。通过Crontab,用户可以轻松地安排脚本或命令在特定时间自动执行,无需手动干预。 #### 二...

    Linux 下定时任务使用详解

    【Linux 下定时任务使用详解】 Linux 系统中的定时任务主要通过 `cron` 服务来实现,它允许用户和系统管理员设置周期性被执行的任务。`cron` 是 Linux 的一个核心组件,但不会自动启动,需要手动操作或设置为开机...

    Linux下的crontab定时执行任务命令详解

    Linux下的crontab定时执行任务命令详解 在Linux操作系统中,`crontab`是一个强大的定时任务调度器,用于在设定的时间间隔执行指定的命令或脚本。它依赖于名为`cron`的守护进程,该进程会在后台持续运行,按照预定义...

    Linux定时任务Crontab详解(推荐)

    通过Crontab,你可以灵活地为Linux系统安排各种定时任务,只需确保正确设置时间参数和指定的命令或脚本路径,即可实现自动化运维。注意在编写Crontab时要遵循正确的格式,避免因语法错误导致任务无法正常执行。

    crontab命令详解

    ### crontab命令详解 #### 一、引言 `crontab` 是 Linux 和 Unix 系统中用于定时执行任务的重要工具。通过 `crontab`,用户可以在指定的时间间隔内自动执行脚本或者命令,这对于自动化运维、数据备份等场景非常...

    crontab 定时执行文件详解

    ### Crontab定时执行工具详解 #### 一、Crontab简介 `cron`是Linux系统中一个重要的后台服务程序,用于实现定时任务的自动化执行。通过`cron`,用户可以方便地设置周期性的任务,比如定时备份数据、清理日志文件等...

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

    ### Linux crontab定时任务配置方法详解 #### CRONTAB概念与介绍 `crontab`是Linux系统中一种非常重要的工具,它主要用于计划周期性的任务执行。通过使用`crontab`,用户可以在特定的时间点自动执行指定的命令或者...

    linux crontab 详解

    ### Linux Crontab 详解 #### 一、Cron与Crontab简介 在Linux系统中,`cron` 是一个后台服务程序(daemon),用于调度周期性任务。它按照预定的时间间隔执行用户定义的任务,例如运行脚本或执行命令等。`crontab` ...

    linux服务器配置定时任务教程

    Crontab是Linux中用于设置定时任务的工具,它的名字由“clock”与“table”两个词合并而来,意为时间表。Crontab可以定义复杂的定时规则,并能够支持各种脚本或命令的执行。 ##### 2.1 Crontab命令 - **crontab -l*...

    linux 定时任务 crontab 详细解释

    ### Linux定时任务Crontab详解 #### 一、Crontab简介 在Linux系统中,Cron服务是一种用于计划执行任务的工具。它允许用户设置周期性运行的任务,例如自动备份数据、定时发送邮件等。Cron服务的核心是`crontab`命令...

    Linux crontab 命令格式与详细例子

    ### Linux Crontab命令详解与应用实例 Cron是Linux系统中的一个守护进程,用于调度周期性执行的任务或脚本。Cron通过读取每个用户的crontab文件来确定哪些任务需要被执行,以及何时执行。本文将深入解析Crontab命令...

    linux如何利用crontab添加定时任务详解

    ### `crontab`命令详解 `crontab`命令有三个主要选项: 1. `-e`: 这个选项用于编辑当前用户的`crontab`文件。它会打开一个文本编辑器(通常是`vim`或`nano`),让用户输入或修改定时任务。 2. `-l`: 使用这个选项...

    Linux定时任务Crontab命令使用详解与总结

    总的来说,Crontab是Linux系统中的强大工具,它简化了定时任务的管理,使得运维人员能够更专注于业务逻辑,而不是繁琐的调度工作。熟练掌握Crontab命令的使用,对于提高工作效率和系统稳定性至关重要。

    Python使用crontab模块设置和清除定时任务操作详解

    ### Python使用crontab模块设置和清除定时任务操作详解 #### 概述 本文主要介绍如何在CentOS 7平台上利用Python与`python-crontab`模块进行定时任务的管理和控制。通过具体的示例,我们将逐步展示从环境搭建到定时...

    linux下定时执行计划任务

    ### Linux 下定时执行计划任务详解 #### 一、概述 在 Linux 操作系统中,定时执行计划任务是一项非常实用的功能,它可以自动地按照预设的时间周期执行特定的任务或脚本,比如定期备份数据、定时清理日志文件等。...

Global site tag (gtag.js) - Google Analytics