Search iEntry News

UnixProNews
SecurityProNews
ITmanagement








CRON – Doing Work On Your Schedule

By Bryan Young
Expert Author
Article Date: 2010-06-29

The cron service is a basic utility in UNIX that provides the ability for different processes to be run at specific times each day.

This can be useful for any number of things, from backing up files to downloading email. How does it know when to run commands and what those commands even are? It's actually a lot simpler than you might think.


Cron, from the Greek word "chronos" for time, is a daemon. In other words, the cron service is always on and listening for a command telling it what to do. In this case, the signal comes from the system clock, which wakes up cron every minute to check if there is anything to do. The way it does this is by checking the crontab configuration file in the /etc directory. If there is a command to be ran, it will be executed, otherwise the service will go back to sleep for another minute until it is reawakened by the system clock.


The crontab file is fairly straightforward in how it works. Looking at the file, you will probably see something like this.


SHELL=/bin/bash


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

MAILTO=root

HOME=/


# run-parts

01 * * * * root run-parts /etc/cron.hourly

02 4 * * * root run-parts /etc/cron.daily

22 4 * * 0 root run-parts /etc/cron.weekly

42 4 1 * * root run-parts /etc/cron.monthly


The top section is setting the variables cron uses. SHELL is the shell cron runs, PATH is where it searches for the commands it uses, MAILTO is where any output will be mailed to , and HOME is the directory used as cron's home directory. Very self-explanatory. The bottom section is the important part. This is where the commands are issued, as well as each command's schedule. The numbers and stars have meaning based on their order. The columns are in this order: minute, hour, day of month, month, day of week, command to be run. Looking at the first line, we can see that each file in /etc/cron.hourly will be run on the 01 minute of every hour of every day of every month on each day of the week. It is important to note that in the day of the week column, Sunday is represented by a 0 and in some versions of UNIX also by a 7.


So now you can run your own cron jobs by adding your own commands to the crontab. You can also put your script in one of the four cron.* sub-directories and it will be executed along with the other files there.

About the Author:
Bryan Young is a staff writer for WebProNews.



Newsletter Archive | Article Archive | Submit Article | Advertising Information | Resources | About Us | Contact

UnixProNews is an iEntry, Inc. ® publication - All Rights Reserved Privacy Policy and Legal