Bryan

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  37 Posts :: 3 Stories :: 24 Comments :: 0 Trackbacks

UnixSolaris环境下建立Cron Jobs

CronUnix, solaris工具, 允许cron精灵进程在后台每隔一段时间自动调度运行任务。这些任务在Unix solaris环境中称为cron jobs, Crontab(Cron)是一个文件,里面包含了要在特定时间运行的Cron的条目。

这个文档中覆盖了Unix cron jobs以下几个方面

1. Crontab限制

2. Crontab 命令

3. Crontab 文件语法

4. Crontab 实例

5. Crontab 环境

6. 禁止发送邮件

7. 产生crontab 行为的Log 文件

1.Crontab限制

如果你的名字在文件/usr/lib/cron/cron.allow.中,那么你可以执行crontab,如果该文件不存在,但是你的名字不在文件/usr/lib/cron/cron.deny,你还是可以使用crontab 如果cron.deny存在并且为空,那么所有人都可以使用crontab,如果文件都不存在,那么只有root用户可以使用crontaballow/deny文件中每行包含一个用户名称。

2Crontab命令

export EDITOR=vi 用于指定一个编辑器来打开crontab文件

crontab -e  编辑crontab文件,如果不存在就创建一个

crontab –l 显示crontab文件内容

crontab –r 移除crontab文件

crontab –v 显示你最后一个编辑crontab的时间(该选项只存在于少量系统中)

3Crontab 文件

Crontab 语法:

Crontab文件有5个字段用于指定天,日期和时间,紧跟着是要运行的命令

上面的值域中的*是指相应的字段所有的合法值。值字段中可以有一个*或者一个逗号分割的元素值的列表。每一个元素或者是上面显示的范围中的一个数字,或者是范围中用-分割的两个数字。

 
*     *     *   *    *        command to be executed
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of week (0 - 6) (Sunday=0)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of        month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

要点

A)每两分钟 /2 10分钟 /10这样的重复匹配模式在很多的操作系统不被支持。 如果你试图使用,而crontab不接受,那么它可能不支持。

B)标准中描述的天可以是以下的两个域,月中的天和周中的天。如果条目中指定了这两个,就显得累積,意味着两个条目都会得到执行。

4Crontab实例

像下面这样Crontab中的文件行意思是在每天下午的6:30移除/home/someuser/tmp中的文件

30     18     *     *     *         rm /home/someuser/tmp/*

如下述列表中一样改变参数的值可以使命令在不同的时间被调度

min

hour

day/month

month

day/week

Execution time

30

0

1

1,6,12

*

– 00:30 Hrs  on 1st of Jan, June & Dec.

 

0

20

*

10

1-5

–8.00 PM every weekday (Mon-Fri) only in Oct.

 

0

0

1,10,15

*

*

– midnight on 1st ,10th & 15th of month

 

5,10

0

10

*

1

– At 12.05,12.10 every Monday & on 10th of every month

:

注意:如果你不经意间输入了没有参数的crontab命令,不要试图按Control-d退出,这样会删除文件中的所有条目,应该使用Control-c退出

5. Crontab环境

Cron使用shell(/usr/bin/sh)从用户的home目录中调用命令。

Cron为每一个shell提供了一个缺省的环境,定义如下

HOME=user’s-home-directory
LOGNAME=user’s-login-id
PATH=/usr/bin:/usr/sbin:.
SHELL=/usr/bin/sh

用户想要执行他们自己的.profile必须显式的在crontab 条目中,或者条目调用的相关脚本中做上面的设置

6.禁止发送邮件

默认情况下,cron jobs发送一封邮件给执行cronjob的用户帐户,如果不需要,在cronjob末尾的文件行键入以下命令

>/dev/null 2>&1

7. 产生相关的log文件

收集cron执行的相关log并输出到文件中

30 18 * * * rm /home/someuser/tmp/* > /home/someuser/cronlogs/clean_tmp_dir.log


Setting up cron jobs in Unix and Solaris

cron is a unix, solaris utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon. These tasks are often termed as cron jobs in unix , solaris.  Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times.

This document covers following aspects of Unix cron jobs
1. Crontab Restrictions
2. Crontab Commands
3. Crontab file – syntax
4. Crontab Example
5. Crontab Environment
6. Disable Email
7. Generate log file for crontab activity

1. Crontab Restrictions
You can execute crontab if your name appears in the file /usr/lib/cron/cron.allow. If that file does not exist, you can use
crontab if your name does not appear in the file /usr/lib/cron/cron.deny.
If only cron.deny exists and is empty, all users can use crontab. If neither file exists, only the root user can use crontab. The allow/deny files consist of one user name per line.

2. Crontab Commands

export EDITOR=vi ;to specify a editor to open crontab file.

crontab -e    Edit your crontab file, or create one if it doesn’t already exist.
crontab -l      Display your crontab file.
crontab -r      Remove your crontab file.
crontab -v      Display the last time you edited your crontab file. (This option is only available on a few systems.)

3. Crontab file
Crontab syntax :
A crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.

*     *     *   *    *        command to be executed
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of week (0 - 6) (Sunday=0)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of        month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

* in the value field above means all legal values as in braces for that column.
The value column can have a * or a list of elements separated by commas. An element is either a number in the ranges shown above or two numbers in the range separated by a hyphen (meaning an inclusive range).
Notes
A. ) Repeat pattern like /2 for every 2 minutes or /10 for every 10 minutes is not supported by all operating systems. If you try to use it and crontab complains it is probably not supported.

B.) The specification of days can be made in two fields: month day and weekday. If both are specified in an entry, they are cumulative meaning both of the entries will get executed .

4. Crontab Example
A line in crontab file like below removes the tmp files from /home/someuser/tmp each day at 6:30 PM.

30     18     *     *     *         rm /home/someuser/tmp/*

Changing the parameter values as below will cause this command to run at different time schedule below :

min hour day/month month day/week Execution time
30 0 1 1,6,12 * – 00:30 Hrs  on 1st of Jan, June & Dec.
0 20 * 10 1-5 –8.00 PM every weekday (Mon-Fri) only in Oct.
0 0 1,10,15 * * – midnight on 1st ,10th & 15th of month
5,10 0 10 * 1 – At 12.05,12.10 every Monday & on 10th of every month
:

Note : 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.

5. Crontab Environment
cron invokes the command from the user’s HOME directory with the shell, (/usr/bin/sh).
cron supplies a default environment for every shell, defining:
HOME=user’s-home-directory
LOGNAME=user’s-login-id
PATH=/usr/bin:/usr/sbin:.
SHELL=/usr/bin/sh

Users who desire to have their .profile executed must explicitly do so in the crontab entry or in a script called by the entry.

6. Disable Email
By default cron jobs sends a email to the user account executing the cronjob. If this is not needed put the following command At the end of the cron job line .

>/dev/null 2>&1

7. Generate log file
To collect the cron execution execution log in a file :

30 18 * * * rm /home/someuser/tmp/* > /home/someuser/cronlogs/clean_tmp_dir.log

 

posted on 2012-06-12 21:49 Life is no respector of any genius. 阅读(363) 评论(0)  编辑  收藏 所属分类: Unix

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问