linux下的crontab和at用法

简单记录下linux下的crontabat用法

简单说下二者区别

at」设定只执行一次的程序

cron 可以用来设定不断的重复定时执行一些工作,然而,如果您只希望在某个时间执行「一次」某个指令,可以使用「at」。

At命令

首先用manat看下基本用法

# man at

NAME

       at, batch, atq, atrm - queue, examine or delete jobs for later execution

 

SYNOPSIS

       at [-V] [-q queue] [-f file] [-mldbv] TIME

       at [-V] [-q queue] [-f file] [-mldbv] -t time_arg

       at -c job [job...]

       atq [-V] [-q queue]

       atrm [-V] job [job...]

       batch [-V] [-q queue] [-f file] [-mv] [TIME]

 

下面再看下和at相关的几个命令

at and batch read commands from standard input or a specified file which are to be executed at a later time .

#at命令是在指定的时间执行某些命令

       at      executes commands at a specified time.

#atq命令是列出当前用户的所有未执行的作业,如果是root用户,那么将列出所有用户的所有未执行作业。输出列表的属性分别为

作业编号,日期,小时,作业类

       atq     lists the user’s pending jobs, unless the user is the superuser; in that case, everybody’s jobs are listed.  The format of the output lines (one for each job) is:

               Job number, date, hour, job class.

#atrm命令是用来删除作业,根据作业编号来进行删除

       atrm    deletes jobs, identified by their job number.

 

再看下命令选项

 

OPTIONS

#-V打印标准错误的版本号

       -V      prints the version number to standard error.

       -q queue

               uses  the  specified queue.  A queue designation consists of a single letter; valid queue designations range from a to z.  and A to Z.  The a queue is the default

               for at and the b queue for batch.  Queues with higher letters run with increased niceness.  The special queue "=" is reserved for jobs which  are  currently  run-

               ning.

 

       If  a job is submitted to a queue designated with an uppercase letter, it is treated as if it had been submitted to batch at that time.  If atq is given a specific queue,

       it will only show jobs pending in that queue.

 

       -m      Send mail to the user when the job has completed even if there was no output.

 

       -f file Reads the job from file rather than standard input.

 

       -l      Is an alias for atq.

 

       -d      Is an alias for atrm.

 

       -v      Shows the time the job will be executed.

 

       Times displayed will be in the format "1997-02-20 14:50" unless the environment variable POSIXLY_CORRECT is set; then, it will be "Thu Feb 20 14:50:00 1997".

 

       -c     cats the jobs listed on the command line to standard output.

 

       -t time_arg

              Submit the job to be run at the time specified by the time_arg option argument, which must have the same format as specified for the touch(1)  utility’s  -t

              time option argument ([[CC]YY]MMDDhhmm).

at」的设定可以分为三个指令:「at」用来建立工作、「atq」用来列出目前待执行的工作有哪些、「atrm」用来删除 atq 中所列出的工作。当您执行了 at 后,它会要求您在命令列中以 shell scripts的写法输入想要执行的指令,而您也可以先将所要执行的指令写再一个档案中,再让 at去执行。

在使用 at 指令时,必须先输入您要在什么时候执行工作,而时间的格式可以是下列任何一种:

格式说明
at 10pm
设定晚上十点执行。时间的格式可以是 HHMM HH:MM
at 8:30am Oct 10
设定十月十日早上八点半执行。
at midnight Jan 1 2005
设定 2005 年一月一日的第一秒钟执行。
at teatime
设定在下次的下午 4 点执行。teatime表示是 4:00pm,而 midnight表示半夜十二点,noon 表示中午十二点。
at -t MMDDhhmm
表示在 MM DD hh mm分时执行,您还可以在 MM 前加上年,而年的格式二位或是四位都可以。

如果您要设定在 10pm 执行某些工作,您可以打「at 10pm」后按 <Enter>,接着您必须开始输入所要执行的指令,在全部输入完成后,请按 <Ctrl>+<D>结束编辑。如果您不想使用命令列编辑的方式输入所要执行的工作,您可以先写一个 shell script 并使用下列指令设定:

# at -f mycommand.sh 10pm

At  18:25 –f  restart_db.sh
上述指令中,您所写的 shell scripts档案是 mycommand.sh

在设定之后,接着您可以使用下列指令列出目前等待执行的 at工作:

# atq
Date                                 Owner          Queue   Job#
2005
6 5日周日 220000 CST    root             c      2
如果您要删除某一个工作,只要使用 atrm 并输入该工作在 atq中的 job id 即可。例如,我们要删除 ID 2 的工作:

# atrm 2
限制一般使用者使用 cron at

大部份的情况下,一般使用者应该不会需要使用定时排程的工作。如果您发现有使用者定时会执行一些耗费系统数据的工作,我们可以为这个指令设限,只允许必要的使用者执行。

 

 

Crontab 用法

 

Mancrontab

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]

 

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 mod-

              ified 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).

    如果是要设定只执行一次的工作,例如,设定在今天 10:00时执行某个指令,我们可以使用「at」这个指令。如果是要设定重复报行的工作,例如,设定每天 12点执行某个指令,我们可以使用「crontab」这个指令,或者是由系统管理者编辑 /etc/crontab这个文件来进行设定。我们先来看看「crontab」重复定时执行程序的说明:

crontab」重复定时执行程序

UNIX 系统中,有一个背景程序会定时执行一些工作,这个程序在 FreeBSD中称为「cron」。「cron」这个程序会定时去检查 /etc/crontab /var/cron/tabs 中的档案,并执行其中的设定。/etc/crontab可以让管理者设定要以什么使用者的身份去执行定时工作,而一般使用者如果要设定定时执行工作时,可以使用指令 crontab -e来编辑自己的定时执行工作,crontab 会将使用者的工作设定放在 /var/cron/tabs中。

我们先来看一下 /etc/crontab的内容说明:

# 设定使用的 shell,路径
SHELL=/bin/sh
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin
#
设定执行指令时的目录
HOME=/var/log
#
当指令有输出数据时,要将输出的东西寄给谁。
MAILTO=""
#
#
分小时天月星期几身份指令
#minute hour mday month wday who command
#
*/5 * * * * root /usr/libexec/atrun

minute:代表一小时内的第几分,范围 0-59
hour
:代表一天中的第几小时,范围 0-23
mday
:代表一个月中的第几天,范围 1-31
month
:代表一年中第几个月,范围 1-12
wday
:代表星期几,范围 0-7 (07都是星期天)
who
:要使用什么身份执行该指令,当您使用 crontab -e 时,不必加此字段。
command
:所要执行的指令。
小时的字段中如果是 *,表示每小时,天的字段中如果是 *,表示每天,依此类推。字段中也可以使用 "-"来表示范围。例如,在小时的字段中填 8-11,表示执行的时间是 8,9,10,11,共四次。

字段中也可以用逗点来表示,以分的字段而言, 1,2,5,9表示将在 1,2,5,9 分时各执行一次。我们也可以写成像这样 1-2,12-14,表示在 1,2,12,13,14 分各执行一次。

另外,也可以用 / 后面加数字表示每几分钟要执行一次。如在分的字段填 0-23/2,表示 1-22分之间,每隔二分钟执行一次,也就是 0,2,4,6,8,10,12,14,16,18,20,22。如果在分的字段是 */5,表示每五分钟一次。

除此之外,在时间的字段中,我们也可以用一个开头为 @的字符串来表示各种排程时间意义:

字符串代表意义
@reboot
开机时跑一次。
@yearly
每年跑一次,等于 "0 0 1 1 *"
@annually
@yearly 一样。
@monthly
每月跑一次,等于 "0 0 1 * *",也就是每月一日半夜 12点执行。
@weekly
每周跑一次,等于 "0 0 * * 0",也就是每个周日半夜 12点执行。
@daily
每天跑一次,等于 "0 0 * * *",也就是每天半夜 12点执行。
@midnight
@daily 一样。
@hourly
每小时跑一次,等于 "0 * * * *"

另外,我们还可以在档案中以「key = value」的方式设定在执行指令时的环境变量。例如,一般指令有输出执行结果时,会自动寄给 root,我们也可以设定「MAILTO = ""」表示不要将输出结果寄出。

以下为几个时间设定的范例:

# 分小时天月星期几身份指令
#minute hour mday month wday who command
#
#
设定每 5 分钟执行一次atrun
*/5 * * * * root /usr/libexec/atrun

# 设定每天一点零一分时执行 /bin/check
1 1 * * * root /bin/check

# 设定每周一 3:11分执行 week_check
11 3 * * 1 root /usr/local/week_check

# 设定每天一点及四点的零到二十三分中间,每二分钟执行一次 something
0-23/2 1,4 * * root /bin/something

# 设定每天半夜十二点执行 something
@daily root /bin/something

如果你以一般使用者或是管理者的身份执行 crontab -e来设定 crontab,你不必设定身份的字段,因为 crontab会自动取得您的身份。使用 crontab -e 所设定的工作会被放在 /var/cron/tabs目录中,所以如果要备份或升级时,应该要注意这些档案是否要备份。

小提示
我们在安排 crontab 时,应该要错开每个程序的执行时间,才不会有一大堆程序同时执行,造成系统负荷过高。

crontab命令使用20070514日星期一上午 10:06
名称 : crontab
使用权限 : 所有使用者
使用方式 :
crontab [ -u user ] file
crontab [ -u user ] { -l | -r | -e }
说明 :
crontab
是用来让使用者在固定时间或固定间隔执行程序之用,换句话说,也就是类似使用者的时程表。-u user是指设定指定 user 的时程表,这个前提是你必须要有其权限(比如说是 root)才能够指定他人的时程表。如果不使用 -u user的话,就是表示设定自己的时程表。
参数 :
crontab -e :
执行文字编辑器来设定时程表,内定的文字编辑器是 VI,如果你想用别的文字编辑器,则请先设定 VISUAL环境变数来指定使用那个文字编辑器(比如说 setenv VISUAL joe)
crontab -r :
删除目前的时程表
crontab -l :
列出目前的时程表
crontab file [-u user]-
用指定的文件替代目前的crontab
时程表的格式如下 :
f1 f2 f3 f4 f5 program
其中 f1 是表示分钟,f2表示小时,f3 表示一个月份中的第几日,f4表示月份,f5 表示一个星期中的第几天。program表示要执行的程序。
f1 *时表示每分钟都要执行 programf2 * 时表示每小时都要执行程序,其馀类推
f1 a-b时表示从第 a 分钟到第 b分钟这段时间内要执行,f2 a-b时表示从第 a 到第 b小时都要执行,其馀类推
f1 */n时表示每 n 分钟个时间间隔执行一次,f2 */n 表示每 n小时个时间间隔执行一次,其馀类推
f1 a, b, c,...时表示第 a, b, c,... 分钟要执行,f2 a, b, c,... 时表示第 a, b, c...个小时要执行,其馀类推
使用者也可以将所有的设定先存放在档案 file 中,用 crontab file的方式来设定时程表。

例子1 :
#
每天早上7点执行一次 /bin/ls :
0 7 * * * /bin/ls
12 月内,每天的早上 6 点到 12点中,每隔3个小时执行一次 /usr/bin/backup :
0 6-12/3 * 12 * /usr/bin/backup
周一到周五每天下午 5:00 寄一封信给 :
0 17 * * 1-5 mail -s "hi" < /tmp/maildata
每月每天的午夜 0 20, 2 20, 4 20....执行 echo "haha"
20 0-23/2 * * * echo "haha"
注意 :
当程序在你所指定的时间执行后,系统会寄一封信给你,显示该程序执行的内容,若是你不希望收到这样的信,请在每一行空一格之后加上 > /dev/null 2>&1即可

例子2 :
#
每天早上610
10 6 * * * date
#
每两个小时
0 */2 * * * date
#
晚上11点到早上8点之间每两个小时,早上8
0 23-7/2
8 * * * date
#
每个月的4号和每个礼拜的礼拜一到礼拜三的早上11
0 11 4 * mon-wed date
#1
月份日早上4
0 4 1 jan * date

crontab

* * * * * scriptsname

前面是时间后面是脚本名


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值