cronjob & crontab

 

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

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

 

 

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

 

 

 

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

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 filenameInstall filename as your crontab file.
crontab -eEdit your crontab file.
crontab -lShow your crontab file.
crontab -rRemove your crontab file.
MAILTO=user@domain.comEmails 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 cronruns, 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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值