From : http://blog.csdn.net/bonny95/archive/2009/09/24/4589069.aspx

 

Cronjob – 快速入门

Cron 是UNIX, SOLARIS,LINUX下的一个十分有用的工具。 通过Cron脚本能使计划任务定期地在系统后台自动运行。这种计划任务在UNIX, SOLARIS, LINUX下术语为cron jobs。 Crontab (CRON 表)则是用来记录在特定时间运行的CRON的一个脚本文件。

Crontab归结为以下几点特性:

1.     Crontab约束

2.     Crontab命令

3.     Crontab文件语法

4.     Crontab示例

5.     Crontab环境设置

6.      邮件

7.      日志文件

8.      其它

 

1.      Crontab约束

当用户名记录在文件/usr/lib/cron/cron.allow时,该用户的crontab有效。当/usr/lib/cron /cron.allow 文件不存在时,用户名没被记录在/usr/lib/cron/cron.deny文件中,该用户的crontab也有效。当只存在/usr/lib /cron/cron.deny且为空时,所有的用户都可使用crontab。当前两个文件都不存在时,只有root用户可以使用crontab。 Deny或allow记录以每行一个用户名的形式记录信息。

2.      Crontab 命令

export EDITOR=vi ;为crontab文件指定一个编辑器。 

crontab  -e  编辑该用户的crontab,当指定crontab  不存在时新建。  

crontab  -l  列出该用户的crontab。  

crontab  -r  删除该用户的crontab。  

crontab  -u<用户名称>  指定要设定crontab的用户名称。

crontab –v 显示上一次编辑的时间(只在某些操作系统上可用)

3.       Crontab 文件

Crontab语法一个crontab文件用五个段来定义:天,日期和时间,和一个要定期执行的命令代码。

 

*     *   *   *    *  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)
 

所有的值都必须在相应的范围之内,否则视为无效。在填值区域内可以是*也可以是以”,”分隔的一组值。值可以是一个数据也可以是用连接符连起来的两个数(表示范围)。注:日期的格式可以是星期,也可以是一个月中的天。假如两个都有值,则在这两个时间都会执行。


4. Crontab 实例 

下面是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退出,这样会删掉所有的cronjob。 应该Control-c退出.

 

 5. Crontab 环境设置

Cron其实是在shell(/usr/bin/sh).后台引用用户程序的命令。

 Cron默认的shell环境是:

HOME=user's-home-directory

LOGNAME=user's-login-id

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

SHELL=/usr/bin/sh  

用户希望自己的配置文件在表或者脚本的入口直接执行。

 6. 禁用邮件默认的cronjobs在执行任务时会给用户发一封邮件 。如果您不需要的话可以通过以下命令关闭。 >/dev/null 2>&1

 7.  生成日志文件保存cronjobs日志只需作如下设置就可以

30 18  *    *   *    rm /home/someuser/tmp/* > /home/someuser/cronlogs/clean_tmp_dir.log “/home/someuser/cronlogs/clean_tmp_dir.log”为日志文件保存的绝对路径  

 

 

 转译英文原文来自

 Copyright © 2000-2007 , Adminschoice.com . All Rights Reserved. Site Comment/Suggestions   Privacy

附上原文地址

http://www.adminschoice.com/docs/crontab.htm

=================================

cronjob & crontab

<script type="text/javascript"> </script> <script type="text/javascript"> </script>

今天要设置一个cron job,查了一些资料,发现了很好的一篇文章,见下文,原文参见:http://www.aota.net/Script_Installation_Tips/cronhelp.php4

但是我按照这篇文章的介绍,在Solaris 9下设置crontab的文件时,老是报错

  1. $ crontab chkbjservers.cron  
  2. MAILTO=xxx@xxxxx.com  
  3. crontab: error on previous line; unexpected character found in  line.  
  4. crontab: errors detected in  input, no crontab file generated.  
 

后来查了Solaris的crontab的手册,发现Solaris的crontab不支持MAILTO,必须要用mailx之类的Unix邮件发送命令。另外,整个cron file只要一句话:minute(s) hour(s) day(s) month(s) weekday(s) command(s). 比如:

 

  1. 30 7 * * 1 /bld/xxxx/mytool/chkbjserver.sh|cat chkbjserver.out |mailx -s "Shared memory checked result in BJ COOL"  xxxx@xxxxxxx.com  
 

=========================================================================

What is a cronjob , and how do I use it?

The cron  daemon is a long running process that executes commands at specific dates and times. To schedule one-time only tasks with cron , use at  or batch . For commands that need to be executed repeatedly (e.g. hourly, daily or weekly), use crontab , which has the following options:

crontab filename Install filename as your crontab  file.
crontab -e Edit your crontab  file.
crontab -l Show your crontab  file.
crontab -r Remove your crontab  file.
MAILTO=user@domain.com Emails the output to the specified address.

The crontab  command creates a crontab  file containing commands and how often cron  should execute them. Each entry in a crontab  file consists of six fields, specified in the following order:

  • minute(s) hour(s) day(s) month(s) weekday(s) command(s)

The fields are separated by spaces or tabs. The first five are integer patterns and the sixth is the command to be executed. The following table briefly describes each of the fields.

FieldValueDescription
minute0-59The exact minute that the command sequence executes.
hour0-23The hour of the day that the command sequence executes.
day1-31The day of the month that the command sequence executes.
month1-12The month of the year that the command sequence executes.
weekday0-6The day of the week that the command sequence executes. Sunday=0, Monday = 1, Tuesday = 2, and so forth.
commandSpecialThe complete command sequence variable that is to be executed.

Each of the patterns from the first five fields may either be 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). Note that the specification of days may be made by two fields (day of the month and day of the week). If both are specified as a list of elements, both are followed. For example:

  • MAILTO=user@domain.com  
    0 0 1,15 * 1 /big/dom/xdomain/cgi-bin/scriptname.cgi

The cron  daemon would run the program scriptname.cgi  in the cgi-bin  directory 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:

  • MAILTO=user@domain.com  
    0 0 * * 1 /big/dom/xdomain/cgi-bin/scriptname.cgi

The program would then only run on Mondays.

If a cron  job specified in your crontab  entry produces any error messages when it runs, they will be reported to you via email.

You may create crontab  files in notepad (being sure to upload them in ASCII) or you may create them within telnet  by simply typing:

  • mcedit cronfile.txt

For more information, consult the man pages. man pages are the directions and tutorials available to you right at the command line from within telnet . Type any of the following lines to open the relevant tutorials:

  • man 5 crontab  {enter} 
    man 1 crontab  {enter} 
    man cron  {enter} 
    man at  {enter} 
    man batch  {enter} 
    man 1 cron  {enter}

Note:  
Your crontab  file must  end with a line feed - in other words, make sure to press [Enter] after the last line in the file.


Try It!

Now that you have read an overview of Cron , test your skills by following the steps below. Once completed you should have a cron  file of your own! 

Step 1:  Create a simple text file using Notepad or any simple text editor that contains the following text:

  • MAILTO=yourusername@yourdomain.com {Enter}
    58 23 * * * /big/dom/xdomain/cgi-bin/yourscript.pl
    {Enter}

Notes for Step 1

  1. You may create this file using your CNC  File Manager by navigating to the /big/dom/xdomain/ directory and clicking 'Create New File' or any other simple text editor such as Notepad.
  2. {Enter} should not actually be typed. [Enter] means hit the {Enter} (return) key to begin the next line and to add a blank line feed at the end of the last line of your Cron  File. It is important to always remember to do this.
  3. MAILTO: Replace the email address with a valid email address of your own. This will ensure that when your cron runs, that any output from the script, such as error message, will be emailed to you.
  4. The second line tells your server when to run this script. In this example, the script will be run at 11:58 PM Eastern Time every day of the year.
  5. It is very important that you double check the script path to ensure it is correct and remember the file names are CaSe SeNsiTive.

Step 2:  Name the text file. (Example: cronfile.txt)
The cronfile  name may be replaced with any name you choose. For instance if you are running a Cron  to trigger an email reminder script it could be called reminder.txt . Many choose to simply call it cronfile.txt. 

Step 3:  Upload the file in ASCII .
Any standard FTP client or your account's CNC  upload feature will work for this. It must be uploaded in ASCII  mode and it is recommended that it be placed in your /big/dom/xdomain/  directory. It may be placed anywhere in your account but to prevent browser access (security risks) it is strongly recommended to place it above your /www directory. 

Step 4:  Telnet  into your account and issue the following command:

  • crontab  /big/dom/xdomain/cronfile.txt

The above tells the server's crontab  where the file is located and that you wish to make it active. Make sure the path to the file is the actual path to where the file was placed. If successful you will be returned to the command bash line, if not an error will be displayed) 

IMPORTANT NOTES  

Removing/Stopping the Cron: Deleting the cronfile.txt  file from your account will not  stop the cron . You may remove this file at any time, since the server's crontab  already has the contents, and the cron  will still run after you have made it active. 

To turn the cron  off you must Telnet  into your account and issue the following command:

The crontab -r  will deactivate the cronjob  and remove the file contents from the server. 

Security Note:  If the script the cron  is setup to run is in the /cgi-bin/ or /www/ directory it may be run at anytime by anyone with browser access. If the crontab  is all that should run the script and you do not want the public to be able to run the script, then you will need to place the script in a directory above the /cgi-bin/ and /www/ directories such as:/big/dom/xdom/user/cronscripts/scriptname

Logo

更多推荐