11.9. I Don't Want to Work Late to Do That Special JobNot 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 JobWhen 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.
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:
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 JobsA 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 SecurityFor any user to access individual batch or at jobs, she needs permission. There are several ways to configure these permissions:
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). |