Scheduling Programs I
Summary:
- What Scheduling Can Do
- 'sleep', 'at', 'cron' and 'anacron'
- 'sleep'
- 'at'
What Scheduling Can Do
You are sitting at your new Linux machine, happily typing away (or grudgingly trying to solve some weird problem ;)), when all of a sudden your hard disk spins up and your system shows all signs of some lively activity going on. After a few minutes, the activity trails off and everything is as it was. You have just encountered your first scheduled 'job'. Job is Unix slang for a program running in the 'background', i.e. without requiring user intervention.
Mandrakelinux comes with several preconfigured schedules which take care of system maintenance: updating the data bank for the 'locate' command, moving the logging files in '/var/log' ('rotating' them), running security checks, removing unused modules from system memory etc. If you are curious, have a look at the directories '/etc/cron.d/' (minute scheduled jobs), '/etc/cron.hourly/' (hourly scheduled jobs), '/etc/cron.daily/' and '/etc/cron.monthly/' which contain the scripts executed by the (ana)cron daemon via '/etc/crontab'.
Of course you can also take advantage of this system, e.g. for
- running regular backups,
- letting the system dial-up and retrieve mail every hour,
- sending yourself reminder messages,
- sending out birthday emails to the right person at the right date,
- removing temporary or unused files,
- scheduling downloads, uploads, mirroring,
- logging off idle users etc. etc.
'sleep', 'at', 'cron' and 'anacron'
These four are responsible for delayed and scheduled command execution:
- sleep suspends a running job for a given period of time.
- at starts a job at a certain time.
- cron runs jobs in fixed intervals.
- anacron runs jobs in flexible intervals.
'sleep'
Unless you are writing your own scripts (or are editing others), 'sleep' won't be of much use to you. In scripts, however, it plays a quite prominent role. Its syntax is
sleep seconds
If you've ever watched the boot messages o an Mandrake Linux system closely, you will have noted the message
Press 'I' to enter interactive startup.
Have you ever managed to press the 'I' key fast enough? Well, I didn't. So I opened the file '/etc/rc.d/rc.sysinit' as 'root' in an editor. I found
if { "$PROMPT" 1.1 = "no" }; then gprintf "\t\tPress 'I' to enter interactive startup." echo -en "\r" echo sleep 1 fi
which says that you have one second (
sleep 1
sleep 5
'sleep' is also used in a number of scripts which start a lot of programs at once to reduce disk usage by adding a pause between the commands.
Example: Pop-up Reminder
You have ten minutes left before you have to leave your computer. You want Linux to tell you when these ten minutes are over.
This either requires 'xmessage' (part of the 'X11R6-contrib' package) or 'gmessage' (part of the 'gtkdialogs' package).
Enter this command into a virtual terminal (or the pop up command line of your window manager):
(sleep 10m; gmessage "Time to leave") &
This will pop up a message box on your desktop in ten minutes.
'at'
'at' adds scheduled jobs to the persistent 'at queue', managed by 'atd', the 'at daemon'. 'atd' must run for 'at' to work properly. You can check that as 'root' with
~# service atd status
You can provide 'at' with absolute times, like 'April 30, 12.00' (at 1200 Apr 30), or with relative times like 'Tomorrow, 2.00 PM' (at 2pm tomorrow) or even 'In 5 hours, 25 minutes from now' (e.g. if it's 4 o'clock in the afternoon: at 0425pm + 5 hours). Valid time options are listed in '/usr/share/doc/at-{...}/timespec'.
Adding a job to the 'at queue' follows a somewhat peculiar scheme. You type in the time and - optionally - date first, hit and then add the command(s) to be executed (one per line). If you're finished, press on an empty line.
at time date
at> command_1
at> command_2
at> ~~
~~warning: commands will be executed using /bin/sh
job number at full time and date
If the scheduled commands produce any output, 'at' will mail it to you.
To get a list of your queued 'at' jobs, run atq, to remove a job from the queue, use atrm job.
Example: Sending yourself a scheduled reminder message
You have to make an important phone call in three days from now on at 4 pm. In order to be reminded of that, tell 'at' to send you a message half an hour before (your mail daemon must be configured properly for this to work):
at 0330pm today + 3 days
at> echo "remember call at 41.1 " | mail your_username@localhost
at>
Notice the 'today' token. Strictly speaking it isn't necessary, but it makes sure that 'at' starts counting from today, regardless of the current time. If you would leave it out and it were past 3.30 pm already, 'at' would start counting from tomorrow at 3.30 pm and you would receive your reminder one day too late.
Administrativa
The 'batch' tool is part of the 'at' package. 'at' jobs invoked by 'batch' are only executed when the system load is below 0.8 (this value can be adjusted with the
atd -l value
command).
The default configuration allows all users to use 'at'. To exclude users, add their login names to '/etc/at.deny' (or add all privileged users to '/etc/at.allow').
Next Item: 'cron', for doing the same things again and again and again ...
Related Resources:
info sleep
man at
~MdkR ef I.4.4
Revision / Modified: Jan. 02, 2002 / Jan. 28, 2002
Author: Tom Berger
Legal: This page is covered by the GNU Free Documentation License. Standard disclaimers of warranty apply. Copyright LSTB and Mandrakesoft.
Version 1.4 last modified by esfa on 21/12/2004 at 13:51
Document data
- Lost account?
- Join the community, be part of the Club: it's free!
- Get the PWP Download Subscription!