System Services - crontab
- 0
- Add a Comment
System Services - crontab
We’re on the last installment of this mini-series on the core system services found on every Linux box. We talked yesterday about cron - the watchful little scheduled tasks workhorse. But like so much else in Linux, cron by itself would have no power. It needs direction. That’s what cron gets from crontab.
The crontab file is, in fact, more of a table than many of the other configuration files. That’s why it’s called crontab instead of cron.conf. A typical crontab entry looks like this:
0 2 * * sun sh listmp3.sh
Let’s break this line up. The first two characters are the minute and the hour that you’d like to carry out a command. In this case, it’s at 2:00 a.m. The next three characters represent more time fields. The first asterisk is the day of the month this command should run. In this case, there’s no day specified. The second asterisk represents the month. The third represents the day. When you tie this in to the 2:00 a.m. entry, you can see that this command should be run at 2:00 a.m. every Sunday. The next chunk of this line tells cron that it should run a shell script [sh] called listmp3.sh. This is a hypothetical script to list all the mp3 files on my machine.
The crontab file is very configurable. If, in this example, I wanted to run the shell script listmp2.sh on the 5th of every month at noon, the entry would look like this:
0 12 5 * * * sh listmp3.sh
The real value in crontab lies in its ability to setup and define maintenance tasks for cron. Here’s a crontab entry that provides a more useful service than just listing my mp3 files:
0 1 * * * find /tmp -atime +3 -exec ls -l {} \;
This entry tells cron to fire off at 1:00 a.m. every day, execute the find program to check in the /tmp directory for any files that haven’t been accessed in more than 3 days, and list them by executing the ls -l command. The {} tells exec to do this for every file found. The \ just tells exec that its work is done.
crontab is a powerful tool for system and routine maintenance. It can help cron fire off programs at scheduled times to complete tasks I’d never remember to do myself.
