Creative time management

In this Daily Feature, Larry Mintz shows you how the crontab file can increase your efficiency, whether you're a sysop or a user.

In this Daily Feature, I’ll show you how to use crontab and at to help automate your computer to make it more efficient. I’ll also look at how to set up cron files in these directories:
  • /etc/cron.hourly
  • /etc/cron.weekly
  • /etc/cron.daily
  • /etc/cron.d

Also, we’ll look at the following files:
  • /etc/at.allow
  • /etc/at.deny

Overview of cron and at
A crontab file has the following format:
minutes hours day-of-month month weekday command

Table A lists time ranges for each of the fields. If you want to leave a blank field in crontab, insert an asterisk (*).

Table A
Field Range
minutes 0-30
hours 0-23
day-of-month 1-31
month 1-12 
weekday 0-6 (0-Sunday)
command N/A

You can use dashes (-) to represent intervals. For example, to indicate the five-hour period from 1 A.M. to 5 A.M. in a crontab file, you’d enter 1-5 in the hours field. Suppose you wanted to cover from 10 A.M. to 4 P.M. in two-hour increments. You’d then use the slash (/) to indicate the desired time division; the entry in the hours field would look like 10-16/2.

The at command accepts the format HH:MM (for hours:minutes). It also accepts terms such as midnight, noon, or teatime. When you’re entering times, keep in mind that the at command accepts the AM or PM designation. You indicate the date by using the form MM/DD/YY or MM.DD.YY. For more information, see the file /usr/doc/at-3.1.7/timespec.

Here’s a sample at command in pseudocode:
at 2pm 4 days from now run fetchmail

At the prompt, you’d type:
at time 2pm+4days fetchmail

Maximizing users’ time
Let’s suppose you’re a user wanting to optimize your time. First, ask your sysop to create a crontab file for you. The sysop will place this file in the /var/spool/cron directory and include your username or something similar in the filename.

Now, let’s say you want to download your e-mail from 10:00 A.M. to 4:00 P.M. at 15-minute intervals every two hours. Using fetchmail, here’s how you’d do it:
#crontab file
15 10-16/2 * * * fetchmail

Now, let’s suppose you want to clean out your Netscape cache files at 10:00, 12:00, 2:00, and 4:00 P.M. The crontab file should look like this:
#crontab file
15 10-16/2 * * * fetchmail
0 10,12,14,16 * * * rm /home/<user>/.netscape/cache/rm *

You can even put a line in the crontab file invoking ical between noon and 4:00 P.M. two times a day on Monday and Wednesday to schedule appointments. So your crontab files now looks like:
#crontab file
15 10-16/2 * * * fetchmail
0 10,12,14,16 * * * rm /home/<user>/.netscape/cache/*
0 12-16/2 mon,wed * * ical

Let’s say you need to back up your work and put it on the archive server. The directory you will place it in is /archives/project/mj_12/<user>.You will use tar on your code in the directory /home/<user>/code. Suppose you want to back up your code three times a day: 11 A.M., 3 P.M., and 5 P.M. Your crontab file should look like this:
#crontab file
15 10-16/2 * * * fetchmail
0 10,12,14,16 * * * rm -rf /home/<user>/.netscape/cache/rm *
0 12-16/2 mon,wed * * ical
0 11,15,17 * * * * backup

Here is the Backup script:#! /etc/ksh
tar cuvf my_code.tar /home/<user>/code/*.c
cp /home/<user>/my_code.tar /archives/

Optimization for sys admins
As a sysop, you have two ways to set up cron tasks. You can centralize them by putting commands in one large crontab file in /var/spool/cron/root, or you break up the tasks you need and place them in the /etc/cron.hourly, /etc/cron.weekly, and etc/cron.monthly files. If you decide to use these files, you’ll have to include symbolic links to software in each of the directories.

For example, suppose you had a macro that called e2fsck_mac and that e2fsck_mac was in /etc/cron.daily. the crontab file would look like:
# /etc/cron.weekly

When @e2fsck_mac is highlighted, you should see this:

Now if you looked in the /var/log/cron file, you should see a line like this:
root (08/10:00-11:00-1234) CMD (run-parts /etc/cron.daily)

Another way to do this is to add a crontab script in the etc/cron.daily directory. It would look like this:
*,*,*,* /usr/sbin/e2fsck -p /dev/hda1

The e2fsck_mac command calls e2fsck -p /dev/hda1.

Next, I’ll show you a simple cron file for automating a sys admin's day. Put the cron file in the /var/spool/cron directory and call it root. Here’s the scenario: Your company has a mail server and a backup server, as well as a work server with workstations. You want to download mail into different mail bins by using fetchmail. The users who will be getting mail are tom_t,mr_big and mr_biggles.

The .fetchmailconf file looks like :
set postmaster "root"
set bouncemail

poll proto pop3
user "tom_t" with pass " coder" is "tom" here
user "mr_big" with pass "ceo" is "jason" here
user "mr_biggles" with pass "money" is "john" here
options fetchall warnings 3600
 antispam 571 550 501 554

The /var/spool/cron/root file should look like this:
#crontab file
0 09-16/2 * * * /sbin/ifup ppp0
15 09-16/2 * * * fetchmail
0 09-16/1 * * * /sbin/ifdown ppp0

The first line calls up every two hours between 9 A.M. and 6 P.M. The second downloads mail in 15-minute intervals between 9 A.M. and 6 P.M. in two-hour intervals. The last line cuts the connection after the first hour.

Now, let’s say some of the duties you have to perform include:
  • Take occasional snapshots of the system using ps.
  • Delete print queues at the end of the day.
  • Monitor users on the system every 30 minutes.
  • Schedule appointments twice a day (on Monday and Friday).
  • Shut down the system at the end of the day.

The crontab file for these actions looks like:
#crontab file
# schedule appointments twice a day on Monday and Friday
0 9,12 * * mon,fri ical
#take occasional snapshots of the system using ps
/30 09-17 * * * ps -ax >> system.dat lpr -P Epson system.dat
#delete print queues at the end of the day
55 17 * * * lpc abort all
# monitor who is on the system every 30 minutes for the whole day
/30 09-17 * * * last>>last.dat|| lpr -P Epson last.dat
#shutting down the system at the end of the day
0 18 * * * shutdown +5 "finish all work now !-SYSTEM shutting
down in 5 minutes"

So to recap, the whole crontab file looks like this:
0 09-16/2 * * * /sbin/ifup ppp0
15 09-16/2 * * * fetchmail
0 09-16/1 * * * /sbin/ifdown ppp0
0 9,12 * * mon,fri ical
/30 09-17 * * * ps -ax >> system.dat| lpr -P Epson system.dat
55 17 * * * lpc abort all
/30 09-17 * * * last>>last.dat| lpr -P Epson last.dat
0 18 * * * shutdown +5 "finish all work now -SYSTEM shutting
down in 5 minutes"

Now let’s look a bit more closely at the /etc/cron.hourly file. You can put the last four cron entries into the /etc/cron.hourly directory. It will look like this:

Each of the scripts contains the commands from the perspective crontab entry. Now, cd into the /etc/crontab file and change the line
01 * * * root run-parts /etc/cron.hourly

*/30 09-17 * * * root run-parts /etc/cron.hourly

In the /etc/cron.daily file, create the script *sys_shutdown. Once again, go into /etc/crontab and change the line
02 4 * * * root run-parts /etc/cron.daily

0 18 * * * root run-parts /etc/cron.daily

Alternatively, you can put the /var/spool/rootcrontab file in the /etc/cron.d directory.

Similarly, if you have weekly tasks, place the scripts in the /etc/cron.weekly directory or create a special crontab file to run within a weekly limit.

Finally, to deny access to at to certain users, place their usernames into the /etc/at.deny file. Conversely, if you want specific people to have access, put their usernames in the /etc/at.allow file.

In this Daily Feature, I demonstrated how to use cron and at to maximize your time. To learn more, see ”Linux + X10 + cron = cheap and simple automation” and”Backup essentials: Safeguarding user data with cron and tar.”

Larry Mintz is a Canadian citizen who does contract work on many levels. Larry holds a B.A. in Mathematics from Concordia University, studies theoretical math, and has written “solution manuals” for many texts.

The authors and editors have taken care in preparation of the content contained herein but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for any damages. Always have a verified backup before making any changes.

Editor's Picks

Free Newsletters, In your Inbox