|
CSC128 : Introduction to UNIX
Using at and crontab
The at Utility
See the man page for at on various options for these commands.
The at utility will allow you to run something 'at' some time.
The usage is:
>at [time] [enter]
command
CONTROL-D
You can also use AM or PM, midnight, noon, or teatime (4pm).
Example:
>at 10:15AM
mail -s 'hey sean, here it is' smauney@parkland.edu < file
CONTROL-D
Unlike crontab you can assume that at starts it's default paths at your
home directory. Even still this can be tricky. Below is an at job that is
correctly placing a listing in a sub-directory of mine:
ls -l ~/public_html/csc128/at/* >~/public_html/csc128/at/listing
Notice that it is ~/public_html (that is tilde slash public_html)
There are also related commands atrm which can be used to remove at jobs from the queue and atq which lists the jobs.
at jobs are placed in the queue by at job mumber for instance:
atq
22 2003-04-23 16:00 a
21 2003-04-23 16:00 a
To view what the contents of an at job is you use the command:
at -c 22
and the shell returns
ls -ila
note how the shell returns the string (in this case ls -ila)that the at
queued job will run at the appointed hour.
cron and crontab
A very common and famous method of running jobs is using cron. cron is the system cronometer. When you are making a cron job you are editing your crontab file. The file is located in different places on different systems. There is a system as well as individual users' crontab files.
To create a crontab file for yourself, simply type
crontab -e
at the command prompt. You will then be in a vi-style editor in which you have 6 fields that you can enter stuff in. When you exit and save (shift ZZ) your crontab file will be saved and will be "installed" in /var/spool/cron/ .
From that time forward the cron daemon will look at the file once per minute to
see if any of the entries (one crontab entry per line) match the criteria to
be run at that time.
1) 2) 3) 4) 5) 6)
minute hour mday month wday command_to run
0-59 0-23 1-31 1-12 0-7 any command
0=sunday
7=sunday also on some systems
1=monday etc
In each of the first 5 fields, a star * is allowable which
is a wildcard, matching all values for that field.
A crontab Entry
Below is a crontab entry that e-mails me a birthday message. Remember to make
your entries so that they are on one line. To do that we use the J command in
vi to join lines.
0 8 26 4 * /usr/bin/mail -s 'Happy Birthday Sean from cron' smauney@parkland.edu </home/staff/smauney/personal/birthdays/sean
The other switches to use with the crontab command are -l which is list your current crontab entries and -d which is remove a users crontab file.
It is important to include full relative or absolute pathnames in cron commands, since the cronjob cannot be assumed to be working out of your home directory.
Output
Both at and cron cannot write to stdout, since the assumption is that you are
not logged in at the time, or are doing something else. By default both at and cron
mail the output to you, you can rely on re-direction to capture a file with the output of
the cron or at job.
When you open or create your crontab file for editing, by using the crontab -e command your crontab file is saved into a directory usually located in /var/cron or /var/spool/cron . Your administrator is in control of these files and directories and you cannot see them directly.
|