Section 11.9. I Don t Want to Work Late to Do That Special Job


11.9. I Don't Want to Work Late to Do That Special Job

Not all jobs have to be run on a regular basis. People who crunch statistical data may need to run scripts at different times. Weathermen who are trying to model future trends may want to try some scripts just once or maybe twice. What they run often takes all of the resources on your systems. The only time they can run their jobs is in the middle of the night. They have families and like to sleep at night, so they may ask you to run that job. Well, perhaps you also have a family and like to sleep at night. But you don't want to create a cron job for this purpose because it's a one-time task.

For this purpose, Linux has the batch-job system, governed by the at daemon. To schedule a batch job at a given time, you can use the at command.

11.9.1. Creating an at Job

When you run the at command to create a batch job, you have to specify a time when the job is to be run. You're then taken to an at> prompt, where you can specify the commands or scripts to be executed.

Users who configure their own scripts can place them in their own ~/bin directory. Scripts in these directories (with executable permission), such as ~/bin/fatdata, can be run without specifying the full path. Debian Linux doesn't add ~/bin to the PATH unless the directory exists.


For example, if you're about to leave for the day and have already configured the fatdata script in your home directory's command bin (~/bin), take the following steps to run the script in one hour:

  1. Run the following command:

     at now + 1 hour 

    The at> prompt is open. If you're in SUSE or Debian Linux, you'll see a note that reflects the default bash shell. (After these steps, I'll describe some alternative ways to specify the time you need.)

  2. At the at> prompt, enter the commands that you want to run at the specified time. In my case, that would be the single command:

     /home/michael/fatdata 

  3. When you're done with the commands that you want run, press Ctrl-D.

  4. If you want to review pending at jobs, use the atq command.

  5. If you want to cancel a job, you can use the atrm command, based on the queue number shown in the output from atq. For example, if you know that your job will be run at 10:30 P.M. tonight, you'll see something similar to the following output from atq, which notes that this is job 7:

     7 2006-01-22 22:30 a michael 

    You can then cancel the job with the atrm 7 command.

As with cron jobs, any output from at jobs is sent via email to the user for whom the job ran.

The at command offers a rich syntax for configuring the job at the time of your choice. While you can specify a certain amount of time in the future, such as:

 at now + 12 hour 

you can also set a specific time, such as 1:00 A.M. tomorrow morning:

 at 1 AM tomorrow 

Alternatively, you can specify a date:

 at 2 AM March 15 

You'll need to make sure the at daemon is running. The following command shows whether it's running:

 ps aux | grep atd 

If it isn't running, make sure it's installed (it's the at RPM or DEB package on our preferred distributions) and configured to run at your default runlevel.

If you want to see how at jobs will be run, you can check them out in your spool. The actual directory varies slightly by distribution: Red Hat/Fedora and Debian use /var/spool/cron/atjobs; SUSE uses /var/spool/atjobs. If you also have batch jobs that use the batch command (see the next section), you'll note that the spool files associated with regular at jobs start with an a, while spool files associated with batch jobs start with a b.

11.9.2. Managing the Load of Your at Jobs

A batch job, in contrast to an at job, runs as soon as the CPU has time for it. All you need to do to create a batch job is to use the batch command. With the batch command, Linux won't run the job unless the load average on the CPU is less than a certain threshold, which depends on the distribution. If you're running Red Hat/Fedora or SUSE Linux, the threshold is .8, or 80 percent of the capacity of a single CPU. If you're running Debian Linux, the threshold is 1.5, or 150 percent of the capacity of a single CPU. Naturally, you'll want to vary this threshold depending on the CPUs on your system.

Except for the aforementioned CPU limits, the batch command works in the same way as the at command; both set you up with an at> prompt. If you want to change the parameters associated with batch jobs, you can do so with the help of the atd command. For example, if your system includes four CPUs, you may find it useful to run batch jobs unless more than three CPUs are loaded:

 atd -l 3 

If your batch jobs are intense, you may want to increase the time between such jobs. By default, they're run in 60-second intervals. The following command increases the interval to one hour:

 atd -b 3600 

11.9.3. Batch Job Security

For any user to access individual batch or at jobs, she needs permission. There are several ways to configure these permissions:

  • If there's an empty /etc/at.deny file (and no /etc/at.allow file), all users are allowed to have individual batch or at jobs.

  • If there are no /etc/at.deny or /etc/at.allow files, only the root user is allowed to have batch or at jobs.

  • If there are specific users in /etc/at.deny, they're not allowed to use batch or at jobs.

  • If /etc/at.deny includes ALL (representing all users) and individual users are listed in /etc/at.allow, only those users listed in the latter file are allowed to have batch or at jobs.

What you do depends on whether some of your users need to create at jobs, and whether they are capable and trusted to do them on their own (or whether you're willing to create them for your users).



Linux Annoyances for Geeks
Linux Annoyances for Geeks: Getting the Most Flexible System in the World Just the Way You Want It
ISBN: 0596008015
EAN: 2147483647
Year: 2004
Pages: 144
Authors: Michael Jang

Similar book on Amazon

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