MandrakeUser.Org - Your Mandrake-Linux Knowledge Base!


 
 

* DocIndex - Administration

Scheduling Programs I

* What Scheduling Can Do
* 'sleep', 'at', 'cron' and 'anacron'
* 'sleep'
* 'at'

Related Resources:

info sleep
man at

MdkR ef I.4.4

Revision / Modified: Jan. 02, 2002 / Jan. 28, 2002
Author: Tom Berger

 

* 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.

Mandrake Linux 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.

In short: everything you have to or want to do regularly or at a certain time on your computer and which requires no interaction, you can let Mandrake Linux do for you.

* section index * top

* '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' is already installed, since it is often used in shell scripts. The 'at' package might not be installed (check with rpm -q at). 'cron' and 'anacron' are included in a standard installation, although 'anacron' might not be started at boot time by default.

* section index * top

* '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" != "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) to press the 'I' key. I changed it to sleep 5, so now Mandrake Linux leaves me five seconds to find the 'I' key and press it.

'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.

* section index * top

* '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 <ENTER> and then add the command(s) to be executed (one per line). If you're finished, press <CTRL d> on an empty line.

at time date <ENTER>
at> command_1<ENTER>
at> command_2<ENTER>
at> <CTRL d>
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 4!" | mail your_username@localhost
at> <CTRL d>

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').

* section index * top

* 'cron', for doing the same things again and again and again ...

 
Legal: All texts on this site are covered by the GNU Free Documentation License. Standard disclaimers of warranty apply. Copyright LSTB (Tom Berger) and Mandrakesoft 1999-2002.