Linux Mall - The Linux SuperStore!
Linux books, Linux CDs, Linux toys, you name it, they have it!

W W W . L I N U X D O T . O R G

Newbie's Linux Manual
Automating Things With Cron
] [ Download*] [ Previous] [ Next] click here.) Home] [ Contents] [ Download*] [ Previous] [ Next] Homepage| The Last 5 Days| The Daily Linux News| The Linux Bits| Newbie's Linux Manual
The Best Linux Sites| Linux Book Reviews| A Windows Vendetta?
Diary of a Linux Newbie| Diary of an Open Source Newbie
The Linux Forum| Just For Fun Amazon - The World's Biggest Bookstore!
4.7 million books, CDs, videos, and DVDs available to buy! Webmaster| Manual's Copyright Terms
[
* In Linux enter: unzip nlm.zip
What Cron Can Do For You

Cron allows users to automate repetitive system administration tasks such as tape backups, database reorganization, and general file cleanups (such as emptying log files and queues).

The Crontab File's Syntax

To tell cron what you want it to run, and how often you want it to run it, you need to create a crontab file. A crontab file is just a text file with the following syntax:

minute hour day-of-month month-of-year day-of-week command

Each of the above columns can be in one of the following formats (these examples are for the minute column):

30
Run command at 30 minutes past the hour.

0-59/10
Run command once every 10 minutes, for the entire hour.

15-30
Run command once every minute, from 15 to 30 minutes past the hour.

0,10,50
Run command at 0 minutes past the hour, 10 minutes past the hour, and 50 minutes past the hour.

*
Run command once every minute.

And here's the range of numbers available for each of the time and date columns:

minute       : 0-59
hour         : 0-23
day-of-month : 0-31
month-of-year: 1-12
day-of-week  : 0-6
(0=Sun, 1=Mon, 2=Tue, 3=Wed, 4=Thu, 5=Fri, 6=Sat)

Here's an example crontab file:


                
30 0 * * * ./backup.sh
0,10,50 9-15 * * * ./compute.sh
0-59/10 * * * 1,3,5 ./netgrab.sh


                

30 0 * * * ./backup.sh
Run the backup.sh script (located in your home directory) at half-past (30) midnight (0), on every day of the month (*), and every day of the year (*), and every day of the week (*).

0,10,50 9-15 * * * ./compute.sh
Run the compute.sh script every 0 minutes, 10 minutes, and 50 minutes past the hours (0,10,50), between 9am and 5pm (9-15), every day of the year.

0-59/10 * * * 1,3,5 ./netgrab.sh
Run the netgrab.sh script every 10 minutes (0-59/10), every Monday, Wednesday, and Friday (1,3,5).

Creating and Submitting Your Crontab File to Cron

There are two methods to create/modify and submit a crontab file:

Method 1

Create a crontab file in a text editor. You can call it whatever you want and save it wherever you like. Now you have to submit the crontab file. To do this enter:

crontab filename

...replacing filename with the location and name of your crontab file e.g. if the file was named crontab and was in your current directory you would enter:

crontab crontab

Method 2

Enter:

crontab -e

To open your default editor (which is Vi). When saving the file just enter :wq without specifying a filename; to quit Vi, and automatically submit the crontab file. (To change your default editor,

Note:

When you sumbit a file, a copy of it is stored in the /var/spool/cron directory, with your username as the filename. So if I submitted the file mycron whilst logged into the account laurence, a copy would be stored as /var/spool/cron/laurence. It's this file the cron daemon uses, and not the master file you created if you used method 1. If you used Method 2 then only one copy of the file exists. Never directly edit files in /var/spool/cron. Cron will not be updated and you can potentially mess things up. Instead use either Method 1 or 2 again, to modify your crontab file.

Yikes, Too Much Mail!

Did I mention that all output from commands, scripts, and error messages are mailed to you? Guaranteed that when you first start using cron you'll think you'd be better off being mail-bombed. To combat this, add a > /dev/null after the command/script, to redirect all output to oblivion.

Listing and Removing Your Crontab File

To display the contents of your crontab file held in the /var/spool/cron directory, enter:

crontab -l

And to remove your crontab file from the /var/spool/cron directory, enter:

crontab -r

Restricting Users

By default only root can submit and modify a crontab file. To allow users to have their own crontab file add their username to the /etc/cron.allow file, e.g:


                
john
julie


                

Indicating that john and julie are now permitted to submit their own crontab file.

Putting Theory Into Practice

Here's a handy little automation exercise for you to try out. It will backup the contents of your home directory to a .tar.gz file each morning at 2.30a.m.

If you haven't already done so, enter:

su -c 'pico /etc/cron.allow'

...and add your username to this file to allow you to submit crontab files.

- 1 -

If you're not already in your home directory, enter:

cd

- 2 -

Enter:

pico backup

...and enter the following:


                
rm backup.tar.gz
tar cfz backup.tar.gz .


                

...then press Ctrl+o to save the file, and Ctrl+x to exit.

- 3 -

Now make the script executable by entering:

chmod +x backup

- 4 -

Enter:

crontab -e

- 5 -

Press i to enter Insert mode and enter the following:


                
30 2 * * * ./backup


                

...then press Esc to return to Command mode, and enter:

:wq

...to exit Vi and submit your crontab file. That's all there is to it. Now you can spend hours thinking of bigger and better ways to make your life as a system administrator an easy one.

And There's More...

There's also directories called /etc/hourly, /etc/daily, /etc/weekly, and /etc/monthly. Simply put an executable file (or a symbolic link to an executable) into the appropriate directory and the script will be executed every hour, day, week, or month. That's all there is to it.

[
* In Linux enter: unzip nlm.zip
© MM Linuxdot.org |