The term Cron comes from the Greek word chronos, which means time. The Linux program that takes care of scheduling is the Cron-daemon. (The word daemon in Unix/Linux-speak is loosely analogous to the an operating system "service" in the Windows world.) A Cron job starts at a pre-selected time. Most are scheduled overnight so not to interrupt normal working hours. (If your computer is off at those times, you'll want to install Anacron, which picks up forgotten jobs the next time the computer boots).
The Cron program in most Linux versions comes with pre-defined system-wide Cron jobs and as a general rule, you shouldn't mess with them. But you can also add a series of root- and user-specific operations. This installment of Linux Explorer will tell you how to do that.
--------------------
IMPORTANT: The tips in this document require the use of command-line statements. For more information about how to read and execute Linux command-line prompts and commands, please check LinuxClues.com's Linux Cheat Sheet:
--------------------
Cron Jobs and the Cron Daemon
The schedule for Cron is written in the /etc/crontab file, so to have a look at the system crontab type:
$ cat /etc/crontab
It should show this:
# run-parts
01 * * * * root nice -n 19 run-parts /etc/cron.hourly
02 4 * * * root nice -n 19 run-parts /etc/cron.daily
22 4 * * 0 root nice -n 19 run-parts /etc/cron.weekly
42 4 1 * * root nice -n 19 run-parts /etc/cron.monthly
The order of codes here is: minute, hour, day of month, month, day of week, user, and command. The * means any value will do. So the first line tells it every 1 minute, of 1 hour, of every day, of every week and every month to run-parts /etc/cron.hourly. The second line tells it to do the daily jobs at 4:02 hours (using the 24 hour clock). The third line tells it to run on day 0 the weekly job at 4:22. (Note: The days are number from 0 to 6, with 0 being Sunday.) The last line will run every 1st of each month at 4:42! If those times are inconvenient, you can change them to your preferences.
Ok, so what is in /etc/cron.daily? Type:
$ cd /etc/cron.daily
$ ls
This will show you this:
0anacron - logrotate - makewhatis.cron - msec - rpm - tmpwatch
Those are all little scripts for the tasks to be done. Just add your script and it will run with the other daily tasks. You can use either shell or bash script, it doesn't really matter.
Here's an example: Have a look at the rpm script now that we are in the right directory. Type:
$ cat rpm
This shows you:
#!/bin/sh
rpm -qa --qf '%{name}-%{version}-%{release}.%{arch}.rpm\n' 2>&1 | sort > /var/log/rpmpkgs
See ? . . . . . . #!/bin/sh on the 1st line showing it's a shell script and a "simple" command on the 2nd line!
Now, you have a better understanding of how Linux works. Explore a little and see what all the hourly to monthly tasks are about.
Cron and Anacron
If you run Mandrake, SUSE, or any other distros and your computer is not powered up 24 hours a day, there are several Cron jobs that might be forgotten because they're scheduled for 3:00AM-4:00AM in the morning. Installing Anacron will set this to rights. Anacron searches for missed Cron jobs and executes them five minutes after you boot your computer. You'll notice extra activity of your CPU and hard disk for about five to seven 7 minutes as a result. Note: Red Hat and Fedora have Anacron pre-installed by default.
Installing Anacron is simple and needs no special configuration. Here's an example of how to install it using Mandrake:
Mandrake Control Center > Software Management > Installing Software > search for Anacron.
If you decide to skip Anacron, keep in mind that you could be running the risk of eventually running out of disk space (in an extreme case). The default logrotate Cron job tries to prevent this by zipping up old logfiles like /var/log/syslog and /var/log/messages. But if your turn your computer off at night, logrotate won't be able to run unless you have Anacron installed.
User-Created Cron Jobs
If you want to set up some tasks yourself (as user or system-wide as root) to run every so many hours/days, here is how to do that:
Make a text file and let's say we call it "test-cron." In the file, place the following lines (replacing "bruno" with what's applicable to your Linux installation):
0 * * * * play /home/bruno/Sound/Hour.wav
30 * * * * play /home/bruno/Sound/HalfHour.wav
You'll need to make sure the sound files named exist in the places indicated above, or change the names to .wav files that are there. Follow the earlier instructions for setting the time and days.
Then type the command:
$ crontab test-cron
And from now on, every hour the Hour.wav will play and every half hour the HalfHour.wav will play, just like grandma's clock. (Your personal cron-settings are then saved in directory comparable to /var/spool/cron/bruno.) You can also do this as root, so it will run for whichever user is logged in at that moment.