Scheduling Tasks


There are three ways to schedule commands in Ubuntu, all of which work in different ways. The first is the at command, which specifies a command to run at a specific time and date relative to today. The second is the batch command, which is actually a script that redirects you to the at command with some extra options set so your command runs when the system is quiet. The last option is the cron daemon, which is the Linux way of executing tasks at a given time.

Using at and batch to Schedule Tasks for Later

If there is a time-intensive task you want to run, but you do not want to do it while you are still logged in, you can tell Ubuntu to run it later with the at command. To use at, you need to tell it the time at which you want to run and then press Enter. You will then see a new prompt that starts with at>, and everything you type there until you press Ctrl+D will be the commands you want at to run.

When the designated time arrives, at will perform each action individually and in order, which means later commands can rely on the results of earlier commands. In this next example, run at just after 5 p.m., at is used to download and extract the latest Linux kernel at a time when the network should be quiet:

[paul@caitlin ~]$ at now + 7 hours at> wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.10.tar.bz2 at> tar xvfjp linux-2.6.10.tar.bz2 at> <EOT> job 2 at 2005-01-09 17:01 


Specifying now + 7 hours as the time does what you would expect: at was run at 5 p.m., so the command will run just after midnight that night. When your job has finished, at will send you an email with a full log of your job's output; type mail at the console to bring up your mailbox and then press the relevant number to read at's mail.

If you have a more complex job, you can use the -f parameter to have at read its commands from a file, like this:

echo wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.10.tar.bz2\; tar xvfjp linux-2.6.10.tar.bz2 > myjob.job at f myjob.job tomorrow 


As you can see, at is flexible about the time format it takes; you can specify it in three ways:

  • Using the now parameter, you can specify how many minutes, hours, days, or weeks relative to the current timefor example, now + 4 weeks would run the command one month from today.

  • You can also specify several special times, including tomorrow, midnight, noon, or teatime (4 p.m.). If you do not specify a time with tomorrow, your job is set for precisely 24 hours from the current time.

  • You can specify an exact date and time using HH:MM MM/DD/YY formatfor example, 16:40 22/12/05 for 4:40 p.m. on the 22nd of December 2005.

When your job is submitted, at will report the job number, date, and time that the job will be executed; the queue identifier; plus the job owner (you). It will also capture all your environment variables and store them along with the job so that, when your job runs, it can restore the variables, preserving your execution environment.

The job number and job queue identifier are both important. When you schedule a job using at, it is placed into queue "a" by default, which means it runs at your specified time and takes up a normal amount of resources.

There is an alternative command, batch, which is really just a shell script that calls at with a few extra options. These options (-q b m now, if you were interested) set at to run on queue b (-q b), mailing the user on completion (-m), and running immediately (now). The queue part is what is important: Jobs scheduled on queue b will only be executed when system load falls below 0.8that is, when the system is not running at full load. Furthermore, it will run with a lower niceness, meaning a queue jobs usually have a niceness of 2, whereas b queue jobs have a niceness of 4.

Because batch always specifies now as its time, you need not specify your own time; it will simply run as soon as the system is quiet. Having a default niceness of 4 means that batched commands will get less system resources than a queue job's (at's default) and less system resources than most other programs. You can optionally specify other queues using at. Queue c runs at niceness 6, queue d runs at niceness 8, and so on. However, it is important to note that the system load is only checked before the command is run. If the load is lower than 0.8, your batch job will be run. If the system load subsequently rises beyond 0.8, your batch job will continue to run, albeit in the background, thanks to its niceness value.

When you submit a job for execution, you will also be returned a job number. If you forget this or just want to see a list of other jobs you have scheduled to run later, use the atq command with no parameters. If you run this as a normal user, it will print only your jobs; running it as a superuser will print everyone's jobs. The output is in the same format as when you submit a job, so you get the ID number, execution time, queue ID, and owner of each job.

If you want to delete a job, use the atrm command followed by the ID number of the job you want to delete. This next example shows atq and atrm being used to list jobs and delete one:

[paul@caitlin ~]$ atq 14      2005-01-20 23:33 a paul 16      2005-02-03 22:34 a paul 17      2005-01-25 22:34 a paul 15      2005-01-22 04:34 a paul 18      2005-01-22 01:35 b paul [paul@caitlin ~]$ atrm 16 [paul@caitlin ~]$ atq 14      2005-01-20 23:33 a paul 17      2005-01-25 22:34 a paul 15      2005-01-22 04:34 a paul 18      2005-01-22 01:35 b paul 


In that example, job 16 is deleted using atrm, and so it does not show up in the second call to atq.

The default configuration for at and batch is to allow everyone to use it, which is not always the desired behavior. Access is controlled through two files: /etc/at.allow and /etc/at.deny. By default, at.deny exists but is empty, which allows everyone to use at and batch. You can enter usernames into at.deny, one per line, to stop those users scheduling jobs.

Alternatively, you can use the at.allow file; this does not exist by default. If you have a blank at.allow file, no one except root is allowed to schedule jobs. As with at.deny, you can add usernames to at.allow one per line, and those users will be able to schedule jobs. You should use either at.deny or at.allow: When someone tries to run at or batch, Ubuntu checks for her username in at.allow. If it is in there, or if at.allow does not exist, Ubuntu checks for her username in at.deny. If her username is in at.deny or at.deny does not exist, she is not allowed to schedule jobs.

Using cron to Run Jobs Repeatedly

The at and batch commands work well if you just want to execute a single task at a later date, but they are less useful if you want to run a task frequently. Instead, there is the crond daemon for running tasks repeatedly based upon systemand userrequests. Cron has a similar permissions system to at: Users listed in the cron.deny file are not allowed to use Cron, and users listed in the cron.allow file are. An empty cron.deny filethe defaultmeans everyone can set jobs. An empty cron.allow file means that no one (except root) can set jobs.

There are two types of jobs: system jobs and user jobs. Only root can edit system jobs, whereas any user whose name appears in cron.allow or does not appear in cron.deny can run user jobs. System jobs are controlled through the /etc/crontab file, which by default looks like this:

SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # m h dom mon dow user command 17 * * * * root run-parts --report /etc/cron.hourly 25 6 * * * root test -x /usr/sbin/anacron || run-parts --report /etc/cron.daily 47 6 * * 7 root test -x /usr/sbin/anacron || run-parts --report /etc/cron.weekly 52 6 1 * * root test -x /usr/sbin/anacron || run-parts --report /etc/cron.monthly 


The first two lines specify which shell should be used to execute the job (defaults to the shell of the user who owns the crontab file, usually /bin/bash), and the search path for executables that will be used. It's important that you avoid using environment variables in this path statement, as they may not be set when the job runs.

The next line starts with a pound sign (#) and so is treated as a comment and ignored. The next four lines are the important parts: They are the jobs themselves.

Each job is specified in seven fields that define the time to run, owner, and command. The first five commands specify the execution time in quite a quirky order: minute (059), hour (023), day of the month (131), month of the year (112), and day of the week (07). For day of the week, both 0 and 7 are Sunday, which means that 1 is Monday, 3 is Wednesday, and so on. If you want to specify "all values" (that is, every minute, every hour, every day, and so on), use an asterisk, *.

The next field specifies the username of the owner of the job. When a job is executed, it uses the username specified here. The last field is the command to execute.

So, the first job runs at minute 17, every hour of every day of every month and executes the command run-parts /etc/cron.hourly. The run-parts command is a simple script that runs all programs inside a given directoryin this case, /etc/cron.hourly. So, in this case, the job will execute at 00:17 (17 minutes past midnight), 01:17, 02:17, 03:17, and so on, and will use all the programs listed in the cron.hourly directory.

The next job runs at minute 25 and hour 6 of every day of every month, running runparts /etc/cron.daily. Because of the hour limitation, this script will run only once per day, at 6:25 a.m. Note that it uses minute 25 rather than minute 17 so that daily jobs do not clash with hourly jobs. You should be able to guess what the next two jobs do, simply by looking at the commands they run!

Inside each of those four directories (cron.hourly, cron.daily, cron.weekly, and cron.monthly) are a collection of shell scripts that will be run by run-parts. For example, in cron.daily you will have scripts like logrotate, which handles backing up of log files, and makewhatis, which updates the whatis database. You can add other system tasks to these directories if you want to, but you should be careful to ensure your scripts are correct.

Caution

The cron daemon reads all the system crontab files and all user crontab files once a minute (on the minute, i.e. at 6:00:00, 6:01:00, and so on) to check for changes. However, any new jobs it finds will not be executed until at least 1 minute has passed.

For example, if it is 6:01:49 (that is, 49 seconds past 1 minute past 6 a.m.) and you set a cron job to run at 6:02, it will not execute. At 6:02, the cron daemon will reread its configuration files and see the new job, but it will not be able to execute it. If you set the job to run at 6:02 a.m. every day, it will be executed the following morning and every subsequent morning.

This same situation exists when deleting jobs. If it is 6:01:49 and you have a job scheduled to run at 6:02, deleting it will make no difference: cron will run it before it rereads the crontab files for changes. However, after it has reread the crontab file and noticed the job is no longer there, it will not be executed in subsequent days.


There are alternative ways of specifying dates. For example, you can use sets of dates and times by using hyphens of commas, such as hours 915 would execute at 9, 10, 11, 12, 13, 14, and 15 (from 9 a.m. to 3 p.m.), whereas 9,11,13,15 would miss out at the even hours. Note that it is important you do not put spaces into these sets because the cron daemon will interpret them as the next field. You can define a step value with a slash (/) to show time division: */4 for hours means "every four hours all day", and 0-12/3 means "every three hours from midnight to noon." You can also specify day and month names rather than numbers, using three-character abbreviations: Sun, Mon, Tue, Fri, Sat for days, or Jan, Feb, Mar, Oct, Nov, Dec for months.

As well as system jobs, there are also user jobs for those users who have the correct permissions. User jobs are stored in the /var/spool/cron directory, with each user having his own file in his named after his usernamefor instance, /var/spool/cron/paul or /var/spool/cron/root. The contents of these files contain the jobs the user wants to run and take roughly the same format as the /etc/crontab file, with the exception that the owner of the job should not be specified because it will always be the same as the filename.

To edit your own crontab file, type crontab e. This brings up a text editor (vim by default, but you can set the EDITOR environment variable to change that) where you can enter your entries. The format of this file is a little different from the format for the main crontab because this time there is no need to specify the owner of the jobit is always you.

So, this time each line is made up of six fields: minute (059), hour (023), day of the month (131), month of the year (112), day of the week (07), and then the command to run. If you are using vim and are new to it, press i to enter insert mode to edit your text; then press Esc to exit insert mode. To save and quit, type a colon followed by wq and press Enter.

When programming, we tend to use a sandbox subdirectory in our home directory where we keep all sorts of temporary files that we were just playing around with. We can use a personal job to empty that directory every morning at 6 a.m. so that we get a fresh start each morning. Here is how that would like in our crontab file:

0 6 * * * rm rf /home/paul/sandbox/*


If you are not allowed to schedule jobs, you will be stopped from editing your crontab file.

Once your jobs are placed, you can use the command crontab l to list your jobs. This just prints the contents of your crontab file, so its output will be the same as the line you just entered.

If you want to remove just one job, the easiest thing to do is type crontab e to edit your crontab file in vim; then, after having moved the cursor to the job you want to delete, type dd (two ds) to delete that line. If you want to delete all your jobs, you can use crontab r to delete your crontab file.



Ubuntu Unleashed
Ubuntu Unleashed 2011 Edition: Covering 10.10 and 11.04 (6th Edition)
ISBN: 0672333449
EAN: 2147483647
Year: 2006
Pages: 318

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net