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 atA 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’ timeLet’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.

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 * * * * backupHere is the Backup script:#! /etc/ksh
tar cuvf my_code.tar /home/<user>/code/*.c
cp /home/<user>/my_code.tar /archives/
project/mj_12/<user>

Optimization for sys adminsAs 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
*makewhatis
@e2fsck_mac

When @e2fsck_mac is highlighted, you should see this:@e2fsck_mac->/usr/sbin/e2fsck_mac

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

The first line calls up total.net 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"

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:#/etc/cron.hourly
*sys_watch
*user_watch

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

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.