Cron Tutorial.pdf

July 24, 2019 | Author: DataStage4You | Category: Operating System Technology, Computing, Technology, Software, Computer Programming
Share Embed Donate


Short Description

Cron...

Description

Cron Tutorial

http://clickmojo.com/code/cron-tutorial.html

www.citibank.co.in

Ads by Google

What is Cron? Ads by Google

PHP T utorial

PHP Cron Job

ANSYS T utorial

AutoCAD 3D T utorial

Cron is a program that enables you to execute a command, or a script with a sequence of commands, at a specified date, time or at set intervals. The commands or scripts that you want cron to run are defined in a file called crontab, and a nd every user has their own independent crontab c rontab file. Cron is a system program that is running all the time and is similar similar to the Windows scheduler which allows allows you to run commands/programs commands/programs at predefined prede fined times and intervals. This tutorial will will explain how to use Cron and crontab cronta b and contains some basic working examples examples that should hopefully illustrate illustrate how h ow it functions.  Note: Cron is only only available on the CGI server. It is not possible possible to run scheduled commands or scripts on the homepages (www) server.

How do I use Cron?

There is a special format for entering ente ring crontabs: Minute Hour Day Month Day Task 

t he hour, 00 to t o 59. * Will indicate indicate every minute (details later) Minute = Minute of the t he day in 24-hour format, 00 to t o 23. * Will indicate indicate every hour (details (det ails later) later) Hour = Hour of the Day = Day of the month, 1 to 31. * Will indicate every day (details later) Month = Month of the year, 1 to 12. * Will indicate every month (details later) Day = Day of the week, 3 chars - sun, mon, tue, or numeric (0=sun, 1=mon etc).... * Will indicate every day (details later) Task  =  = The command you want to execute  Note: each of the above must be separated separate d by at least 1 space. spac e. It is advised to include the following line, line, or similar similar at the top of the t he file: [email protected]

This ensures any error output from the cron tasks and any output from your script gets emailed to an address a ddress you can pick it up from. If you do not add this t his entry, any errors our script output will will be appended to a file called Mailbox in in your CGI user account ac count home directory. This insures that you can ca n easily find and resolve any  problems that occur when running a command command or script through through cron. To stop any email or Mailbox Mailbox file file being generated when a command is executed, use MAILTO="". This should only be used once you have established the comm c ommands ands specified in your crontab c rontab file are working correctly. Absolute Pathnames

When entering ent ering commands commands into the crontab file, file, it is important that you use absolute pathnames (such as a s "/files /home1/username/script.php") to specify the script in crontab. This is also true for any local scripts you use within other scripts and for any system commands you use within within the script you are running. running. This is because when cron runs your command, it does not have the t he same $PATH $P ATH defined for finding system commands. To find

1 f3

10/11/2010 6 02 PM

Cron Tutorial

http://clickmojo.com/code/cron-tutorial.html

the path to a system command, just enter whereis command at the $ prompt and you will get a path to the command returned, which you can then use within your script. One of the common failures failures is not using an absolute path when run from cron. Even though t hough it runs perfectly when you run it from the $ prompt. You can use the environment variable $HOME to simplify simplify the path to t o files and scripts within your home directory as it corresponds to the full path to your y our home directory. So instead instead of using /files/home1/username /files/home1/username /php/script.php you can use $HOME/php/script.php. So, an example crontab may look like: [email protected] * * * * * /command/to/execute This would would execute exec ute /command/to/execute every minute.  Now that you have a basic understanding of how cron works, we will will expand on it with some some examples.

Examples

How do I run a task every 5 minutes? One option is to use [email protected] 0,5,10,15,20,25,30,35,40,45,50,55 * * * * /command/to/execute However, there is a special shortcut for this: [email protected] */5 * * * * /command/to/execute The */5 is known as a short form equivalent to 0,5,10,15,20 etc... and achieves the same effect as the previous example, executing the command every 5 minutes. Other examples are: */2 would be every 2 mins, mins, */30 every ev ery 30 minutes and so on. You can use the same short form for the hour indicator */2 every 2 hours, */6 every 6 hours etc. How do I run a task at 6PM every night? [email protected] 00 18 * * * /command/to/execute

How do I run a php script at 2am every Sunday? Sunday? [email protected] 00 02 * * sun /usr/local/bin/php /u sr/local/bin/php $HOME/php/script.php $HOME/php/script.php  Notice that the php command is specified specified using an absolute path because be cause cron will not be able to find it it otherwise. If it was not specified, the script.php will will fail to execute and an error e rror like: php not found will will be reported in the email you receive or in Mailbox. You may may not spot this, especially if you run it successfully successfully from the $ prompt as php /absolute/path/to/script.php or even php script.php if it is in the current directory. Also note $HOME is being used instead of /files/homeX/username/ Example MailBox or Email from cron

2 f3

10/11/2010 6 02 PM

Cron Tutorial

http://clickmojo.com/code/cron-tutorial.html

tutorialsteam@shell2 tutorialsteam@shell2 tutorialsteam $ more Mailbox From [email protected] Sun Feb 01 23:11:27 2004 Return-Path: Delivered-To: [email protected] Received: (qmail 18527 invoked by uid 10667); 1 Feb Fe b 2004 23:11:25 -0000 Date: 1 Feb 2004 23:11:25 -0000 Message-ID: From: [email protected] (Cron Daemon) To: [email protected] Subject: Cron $HOME/me X-Cron-Env: X-Cron-Env: e2/tutorialsteam> X-Cron-Env: n> X-Cron-Env: I am: uid=10667(tutorialsteam) gid=500(shellcgi) groups=500(shellcgi) PATH is set to: /usr/bin:/bin

Crontab command options crontab -e

As explained earlier, this will will allow allow you to edit the contents of your crontab file or create crea te a new crontab file if  one does not already exist. The editor used is called vi or vim. crontab -l

This will will list list the current contents of your crontab file and is very useful for checking chec king you have edited it correctly after crontab -e. It is often useful to make a copy of the crontab file in case you make a mistake with an edit. $ crontab -l >mycrontab This will will create a local copy of the crontab c rontab file called mycrontab. crontab -r

Use Use with caution: caut ion: This This will will delete the contents cont ents of your current curre nt crontab fil f ilee (another reason for making a local copy!) crontab file

This is is an alternative alternat ive method for setting up your crontab file. Instead of using crontab -e, -e , you can ca n create a file containing the cron commands and use that to replace or overwrite the current contents of your crontab file.  Note replace - it will overwrite anything that is currently in your crontab file with the contents of file. expired expire d do mains

3 f3

10/11/2010 6 02 PM

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF