Вы находитесь на странице: 1из 9

Cron

What is cron or crontab?


 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 every user has their own
independent crontab file.
How do I use Cron?

 Syntax of Cron:
 [min] [hour] [day of month] [month] [day of week] [program to be
run]
 [min] Minutes that program should be executed on. 0-59. (Do
not set as * or the program will be run once a minute).
 [hour] Hour that program should be executed on. 0-23. (* for
every hour).
 [day of month] Day of the month that process should be
executed on. 1-31. (* for every day).
 [month] Month that program whould be executed on. 1-12 (* for
every month).
 [day of week] Day of the week. 0-6 where Sunday = 0, Monday
= 1, ...., Saturday = 6. (* for every day of the week).
 [program] Program to be executed. (Include full path
information).
Mailing to user
 MAILTO=cron@username.plus.com

 Example
 MAILTO=NOC@marketlive.com
Crontab commands
options
 crontab –e
 As explained earlier, this will allow you to edit the contents of your crontab file or
create a new crontab file if one does not already exist. The editor used is called vi or
vim.
 crontab -l
 This will list the current contents of your crontab file and is very useful for checking 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 create a local copy of the crontab file called mycrontab.
 crontab -r
 Use with caution: This will delete the contents of your current crontab file (another
reason for making a local copy!)
 crontab file
 This is an alternative method for setting up your crontab file. Instead of using crontab
-e, you can 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.
Examples
 crontab -e [username]
 To schudle the job for particular
user(msuser)
 Crontab –u [username] –l
 To list the jobs under username
Editing crontab to schedule
the jobs
 $Crontab –u msuser –e
 Example
 Scheduling copying the files from /root to /mnt file.
 10 23 21 03 05 /bin/cp /root /mnt

 This schedule will be occur at 22/03 @ 11.10pm IST


 Few more examples
 How do I run a task every 5 minutes?
 One option is to use
 MAILTO=cron@username.plus.com
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /command/to/execute
 However, there is a special shortcut for this:
 MAILTO=cron@username.plus.com
*/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, */30 every 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?
 MAILTO=cron@username.plus.com
00 18 * * * /command/to/execute
 How do I run a php script at 2am every Sunday?
 MAILTO=cron@username.plus.com
00 02 * * sun /usr/local/bin/php $HOME/php/script.php
Batch jobs setup and
configuration

Вам также может понравиться