sco linux crontab重启,crontab命令详细解析

分类:

2011-08-05 19:16:21

crontab命令的功能是在一定的时间间隔调度一些命令的执行。在/etc目录下有一个crontab文件,这里存放有系统运行的一些调度程序。每个用户可以建立自己的调度crontab。

crontab命令有三种形式的命令行结构:

crontab [-u user] [file]

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

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

命令行中-r选项的作用是从/usr/spool/cron/crontabs目录下删除用户定义的文件crontab;

命令行中-l选项的作用是显示用户crontab文件的内容。

使用命令crontab -u user -e命令编辑用户user的cron(c)作业。用户通过编辑文件来增加或修改任何作业请求。

执行命令crontab -u user -r即可删除当前用户的所有的cron作业。

作业与它们预定的时间储存在文件/usr/spool/cron/crontabs/username里。username使用户名,在相应的文件中存放着该用户所要运行的命令。命令执行的结果,无论是标准输出还是错误输出,都将以邮件形式发给用户。文件里的每一个请求必须包含以spaces和tabs分割的六个域。前五个字段可以取整数值,指定何时开始工作,第六个域是字符串,称为命令字段,其中包括了crontab调度执行的命令。

第一道第五个字段的整数取值范围及意义是:

0~59 表示分

1~23 表示小时

1~31 表示日

1~12 表示月份

0~6 表示星期(其中0表示星期日)

/usr/lib/cron/cron.allow表示谁能使用crontab命令。如果它是一个空文件表明没有一个用户能安排作业。如果这个文件不存在,而有另外一个文件/usr/lib/cron/cron.deny,则只有不包括在这个文件中的用户才可以使用crontab命令。如果它是一个空文件表明任何用户都可安排作业。两个文件同时存在时cron.allow优先,如果都不存在,只有超级用户可以安排作业。

原来英文版crontab命令解析

User Commands                                          crontab(1)

NAME

crontab - user crontab file

SYNOPSIS

crontab [filename]

crontab [-elr username]

DESCRIPTION

The crontab utility manages a user's access with  cron  (see

cron(1M))  by copying, creating, listing, and removing cron-

tab files. If invoked without options,  crontab  copies  the

specified  file,  or the standard input if no file is speci-

fied, into a directory that holds all users' crontabs.

If crontab is invoked with filename, this will overwrite  an

existing crontab entry for the user that invokes it.

crontab Access Control

Users: Access to crontab is allowed:

o  if the user's name appears in /etc/cron.d/cron.allow.

o  if  /etc/cron.d/cron.allow  does  not  exist  and  the

user's name is not in /etc/cron.d/cron.deny.

Users: Access to crontab is denied:

o  if /etc/cron.d/cron.allow exists and the  user's  name

is not in it.

o  if /etc/cron.d/cron.allow does not  exist  and  user's

name is in /etc/cron.d/cron.deny.

o  if  neither  file  exists,  only  a  user   with   the

solaris.jobs.user authorization is allowed to submit a

job.

o  If BSM audit is  enabled,  the  user's  shell  is  not

audited  and  the  user is not the crontab owner. This

can occur if the user logs in via a program,  such  as

some versions of SSH, which does not set audit parame-

ters.

Notice that the rules for allow and deny apply to root  only

if the allow/deny files exist.

The allow/deny files consist of one user name per line.

crontab Entry Format

A crontab file consists of lines of  six  fields  each.  The

fields  are  separated by spaces or tabs. The first five are

SunOS 5.9           Last change: 19 Apr 2002                    1

User Commands                                          crontab(1)

integer patterns that specify the following:

minute (0-59),

hour (0-23),

day of the month (1-31),

month of the year (1-12),

day of the week (0-6 with 0=Sunday).

Each of these patterns may be either an  asterisk   (meaning

all legal values) or a list of elements separated by commas.

An element is either a number or two numbers separated by  a

minus  sign  (meaning  an  inclusive range). Notice that the

specification of days may be made by two fields (day of  the

month and day of the week). Both are adhered to if specified

as a list of elements. See EXAMPLES.

The sixth field of a line in a crontab file is a string that

is  executed  by the shell at the specified times. A percent

character in this field (unless escaped by \) is  translated

to a NEWLINE character.

Only the first line (up to a `%' or end of line) of the com-

mand  field  is  executed by the shell. Other lines are made

available to the command as standard input. Any  blank  line

or  line  beginning  with  a  `#'  is  a comment and will be

ignored.

The shell is invoked from your $HOME directory with an  arg0

of sh. Users who desire to have their .profile executed must

explicitly do so  in  the  crontab  file.  cron  supplies  a

default environment for every shell, defining HOME, LOGNAME,

SHELL(=/bin/sh), TZ, and PATH. The  default  PATH  for  user

cron  jobs  is  /usr/bin;  while  root  cron jobs default to

/usr/sbin:/usr/bin.  The  default  PATH  can   be   set   in

/etc/default/cron (see cron(1M)).

If you do not redirect  the  standard  output  and  standard

error  of your commands, any generated output or errors will

be mailed to you.

OPTIONS

The following options are supported:

-e    Edits a copy of the current user's  crontab  file,  or

creates  an  empty  file  to  edit if crontab does not

exist. When editing is complete, the file is installed

as  the  user's  crontab file. If a username is given,

the specified user's crontab file  is  edited,  rather

than the current user's crontab file; this may only be

done by a user with the solaris.jobs.admin  authoriza-

tion.   The  environment  variable  EDITOR  determines

which editor  is  invoked  with  the  -e  option.  The

SunOS 5.9           Last change: 19 Apr 2002                    2

User Commands                                          crontab(1)

default  editor is ed(1). Notice that all crontab jobs

should be submitted using crontab. Do not add jobs  by

just  editing  the crontab file, because cron will not

be aware of changes made this way.

If all lines in the crontab file are deleted, the  old

crontab  file  will  be  restored.  The correct way to

delete all lines is to remove the crontab file via the

-r option.

-l    Lists the crontab file for the invoking user.  Only  a

user  with  the  solaris.jobs.admin  authorization can

specify a username following the -r or -l  options  to

remove or list the crontab file of the specified user.

-r    Removes a user's crontab from the crontab directory.

EXAMPLES

Example 1: Cleaning up core files

This example cleans up core files every weekday  morning  at

3:15 am:

15 3 * * 1-5 find $HOME -name core 2>/dev/null | xargs rm -f

Example 2: Mailing a birthday greeting

0 12 14 2 * mailx john%Happy Birthday!%Time for lunch.

Example 3: Specifying days of the month and week

This example

0 0 1,15 * 1

would run a command on  the  first  and  fifteenth  of  each

month, as well as on every Monday.

To specify days by only one field, the other field should be

set to *. For example:

0 0 * * 1

would run a command only on Mondays.

ENVIRONMENT VARIABLES

See environ(5) for descriptions of the following environment

variables  that  affect  the  execution  of  crontab:  LANG,

LC_ALL, LC_CTYPE, LC_MESSAGES, and NLSPATH.

EDITOR

Determine the editor to be invoked when the -e  option

SunOS 5.9           Last change: 19 Apr 2002                    3

User Commands                                          crontab(1)

is specified. The default editor is vi(1).

EXIT STATUS

The following exit values are returned:

0     Successful completion.

>0    An error occurred.

FILES

/etc/cron.d

main cron directory

/etc/cron.d/cron.allow

list of allowed users

/etc/default/cron

contains cron default settings

/etc/cron.d/cron.deny

list of denied users

/var/cron/log

accounting information

/var/spool/cron/crontabs

spool area for crontab

ATTRIBUTES

See attributes(5) for descriptions of the  following  attri-

butes:

____________________________________________________________

|       ATTRIBUTE TYPE        |       ATTRIBUTE VALUE       |

|_____________________________|_____________________________|

| Availability                | SUNWcsu                     |

|_____________________________|_____________________________|

| Interface Stability         | Standard                    |

|_____________________________|_____________________________|

SEE ALSO

atq(1), atrm(1), auths(1), sh(1), vi(1),  cron(1M),  su(1M),

auth_attr(4), attributes(5), environ(5), standards(5)

NOTES

If you inadvertently  enter  the  crontab  command  with  no

argument(s),  do not attempt to get out with Control-d. This

removes all entries in your crontab file. Instead, exit with

Control-c.

SunOS 5.9           Last change: 19 Apr 2002                    4

User Commands                                          crontab(1)

If an authorized user modifies another user's crontab  file,

resulting   behavior  may  be  unpredictable.  Instead,  the

super-user should first use su(1M) to become  super-user  to

the  other  user's  login  before  making any changes to the

crontab file.

When updating cron, check first for existing crontab entries

that  may be scheduled close to the time of the update. Such

entries may be lost if the update  process  completes  after

the  scheduled  event. This can happen because, when cron is

notified by crontab to update the internal view of a  user's

crontab  file, it first removes the user's existing internal

crontab and any internal scheduled events. Then it reads the

new  crontab  file  and  rebuilds  the  internal crontab and

events. This last step takes time, especially with  a  large

crontab  file,  and  may  complete after an existing crontab

entry is scheduled to run if it is scheduled  too  close  to

the  update. To be safe, start a new job at least 60 seconds

after the current date and time.

SunOS 5.9           Last change: 19 Apr 2002                    5

作业自动调度(cron)

SCO UNIX系统启动时,会自动执行cron命令,在系统中产生一个cron精灵

进程(或称守护进程)。该进程将自动调度相关作业并使其在后台运行。

cron精灵进程可按照预先设定的时间或时间间隔自动执行用户提交的作业。

如果想了解cron进程的活动情况,在/etc/default/cron 中,将CRONLOG设置为

YES,该值默认为NO,系统将自动生成相应的日志文件/usr/lib/cron/log。

在/etc/default/cron中MAXLOGSIZE 限制了log文件的大小,该变量以512字

节为单位设定的log的最大块数,确省为2048块。

在/etc/default/cron中的MAXCRON变量,控制在同一时刻允许运行的最大

进程数量,确省为100。

提交作业的命令有三个: at,batch和cron。通常 at 和 batch 用于只运行

一次的作业提交。crontab 用于按照一定时间反复运行的作业。

一。at 和 batch 的使用

at 命令需要指定命令执行的时间。而batch命令则无需指定执行时间(相应

命令只有在系统负载允许时才能执行)。

语法如下

at [ -m ] [ -f file ] [ -q letter ] time [ date ] [ increment ]

at [ -m ] [ -f file ] [ -q letter ] -t [[CC]YY]MMDDhhmm.[SS]

at -r job-id ...

at -l [ job-id ... ]

at -l -q letter

batch

相关文件与目录

/usr/lib/cron

main cron directory

/usr/lib/cron/at.allow

list of allowed users

/usr/lib/cron/at.deny

list of denied users

/usr/lib/cron/queuedefs

scheduling information

/usr/spool/cron/atjobs

spool area

/usr/lib/cron/.proto

at and batch environment information

/usr/lib/cron/.proto.a

at shell environment

/usr/lib/cron/.proto.b

batch shell environment

/usr/lib/cron/.proto

at and batch environment information

/usr/lib/cron/.proto.a

at shell environment

/usr/lib/cron/.proto.b

batch shell environment

部分参数说明分别如下

1)time

表示时间,可指定为1,2,或4个数字,1或2个数字被认为是小时,4个数字

被认为是小时和分钟,格式为 hh:mmm

2)date

表示日期

3)increment

表示要在前面所指定的时间(time[date])再推后一段时间

4)-t [[cc]YY]MMDDhhmm.[SS]

CC  世纪

YY  年

MM  月

DD  日

hh  时

mm  分

SS  秒

5)-q letter

把指定作业置于指定字母所指示的队列内,可以是从a到z的任何小写字母

以下队列有特殊意义

a  at 队列

b  batch 队列

c  cron 队列

6)-m

不使用-m 选项时,作业的标准输出将直接mail给相应用户(除非作业被

明确重新定向)。使用-m 选项时,即使作业没有输出,也将以邮件形式

通知用户。

7)-f file

指定包含有at 作业的源文件名,以代替从标准输入。

8)-l [job-id]

列出指定作业的相关信息

9)-r job-id

删除指定作业

at 和 batch 作业提交后,均被指定一个相应的作业标识符,at作业带有

后缀.a, batch作业带有后缀 .b 。所有作业以其作业标识符为文件名,保存

在/usr/spool/cron/atjobs目录中。

二。crontab 的使用

crontab 提交的命令主要用于定期执行某些任务

用户提交的cron作业,都保存在相应的/usr/spool/cron/crontabs/username文件中

语法如下

crontab [ file ]

crontab [ -e | -l | -r ]

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

相关文件目录

/usr/lib/cron

main cron directory

/usr/spool/cron/crontabs

crontab directory

/usr/lib/cron/cron.allow

list of allowed users

/usr/lib/cron/cron.deny

list of denied users

/usr/lib/cron/queuedefs

cron data file

部分参数说明分别如下

1) crontab [cronfile]

(不建议使用该方式,如果用crontab cronfile ,将会把原来的已经

提交的cron作业删除)

cronfile 是具有一定格式的普通文件,在文件中可以包含多余一个的

作业信息,每个作业作为一个登记项,独占一行,用空格分开的6个字

段,具体格式如下

minutes hours day_of_month month day_of_week command

minutes           分钟     0-60

hours             小时     0-23

day_of_month      日       1-31

month             月       1-12

day_of_week       星期的日 0-6(0表示星期日)

command           命令     任何非交互的命令

2) -l

显示已经提交的cron 作业

3)-e

对已经提交的cron 作业编辑,或添加,删除作业

建议使用该方式添加cron作业,

相比手工修改/usr/spool/cron/crontabs/username文件,使用

crontab -e 可以对提交的作业进行语法检查,如果没有按照规定6个

字段提交作业,会提示相应错误信息。

如果手工对/usr/spool/cron/crontabs/username 编辑,为确保作业

提交可按照以下步骤执行

用crontab -e 编辑该文件 :x存盘退出

用ps 命令查找cron进程,用kill 杀下/etc/cron进程,

rm /usr/lib/cron/FIFO

/etc/cron

注释: /usr/lib/cron/FIFO,当新的作业提交由crontab完成时,一个

发送消息到cron守护进程的命令管道。

4) -r 删除当前已经提交的cron 作业

5) -u user

对指定用户的cron 作业进行维护。

三。相关维护

/usr/lib/cron目录下的

at.allow 记录了允许使用at和batch命令的用户

at.deny 记录了禁止使用at和batch命令的用户

cron.allow  记录了允许使用crontab 命令的用户

cron.deny  记录了禁止使用crontab 命令的用户

1) 如果只存在 .allow ,则只有包括在该文件中的用户可以执行相应命令

,如果该文件为空,表明没有一个用户能执行相应命令。

2) 如果不存在.allow ,只存在 .deny ,则只有不在该文件中的用户可以

执行相应命令。如果该文件为空,表明所有用户都可以执行相应命令

3)  如果.allow .deny 都存在,则 .allow 享有优先权

4) 如果.allow .deny 都不存在,则只有超级用户才能执行相关命令。

环境文件

/usr/lib/cron/.proto定义的shell环境适用于 at和batch 。

/usr/lib/cron/.proto.a 适用于at

/usr/lib/cron/.proto.b 适用于batch

.proto.a ,.proto.b 与 .proto相比有更高的优先权。

默认情况下,只存在 .proto.b 和 .proto ,且内容相同。

以上相关维护可以使用 cron manager 实现

cron作业提交后,未能定时完成相应任务,可按照下面的方法查找原因

1。查看 /usr/lib/cron/log 中的提示

2。将提交的作业输出重新定向,查看相应的log文件

作业提交后未能完成相应任务,通常是因为环境变量设置的问题

例如:因PATH等变量的设置  未找到相关的程序等

阅读(1080) | 评论(0) | 转发(0) |

给主人留下些什么吧!~~

评论热议

请登录后评论。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值