Schedule Jobs With AT

Posted by JD 03/04/2011 at 04:00

On UNIX/Linux operating systems, you can easily schedule jobs to be performed later with at. At is like cron, but more flexible for 1-time tasks to be run later.

echo "wget download-some-file-later" | at now + 1 day

This command will take whatever the current time is and add 24 hours to it, then it will run the wget command provided. Time specifications are pretty easy to use. A few more examples:

echo "wget download-some-file-later" | at now + 2 months
echo "wget download-some-file-later" | at now + 1 year
echo "wget download-some-file-later" | at Friday
echo "wget download-some-file-later" | at Tuesday
echo "wget download-some-file-later" | at 6/1/11
echo "wget download-some-file-later" | at 5 pm 3/1/11

More Time Specifications

Some timespecs that work:
midnight, noon, teatime, tomorrow, today, Mon, Tue, Wed, Fri … , or Jan, Feb, Jun, Jul, Sep … you get the idea.

at receives the command to run from STDIN, but the -f switch can be more convenient if you have a script.

at -f ./script.sh 3 pm tomorrow
at -f ./script.sh 3 pm + 1 day
at -f ./script.sh now + 27 hours
at -f ./script.sh now + 1 day
at -f ./script.sh noon Friday

(assuming today is Thursday)

What commands can you use? Anything. Play a song, start an application, send an email, download a file or 1,000. Clean up files, rename files, restart your system. Anything. You can even use at to schedule other at jobs.

The output will be spooled to your email automatically, assuming your system is setup to send email (postfix/sendmail). On my systems, I setup local account email to be forwarded to my domain email address automatically in the /etc/aliases file.

Some systems don’t allow all users to run at jobs, so check your local settings for at and cron access. You will get an error if you aren’t permitted.

Since at works like a job scheduling tool, you can see the queue with atq or remove specific jobs with atrm as needed.

“Nice” Level Control

echo "mkv-convert file-03.mpg"|at -q z now

The process priority is controlled by the queue tag. “z” as shown above uses a nice level of +19, a low priority. Using “a” on my system set the priority to +12, but I already had a few tasks running at nice +10. Normal tasks run at nice level of “0”, as a reminder. For batch jobs, having a lower default nice level is smart and at does that automatically.

Batch Control and Queues

If I had 50 files to convert, I’d use batch, not at

echo "mkv-convert file-01.mpg"|batch
echo "mkv-convert file-02.mpg"|batch
.
.
echo "mkv-convert file-50.mpg"|batch 

Ok, probably not really, since I love task spooler already. TS takes all the output and stores it in a log file. It doesn’t automatically email the file like batch or at will. The downside to ts is you won’t find it in any official Ubuntu repositories to my knowledge. Batch is a reasonable alternative that only runs jobs when the CPU can support them. It isn’t perfectly clear when a job scheduled with batch will be allowed to run. The man page says when the CPU load is less than 1.5 and that atd controls which value is actually used. Some of my systems have 4 cores, so perhaps that value should be 3?

Man Pages Rock

As always, the man pages provide more details on these commands and configuration files.

  • man at
  • man atq
  • man atrm
  • man batch
  • man at.allow
  • man at.deny

If you are on OSX, I suppose this will work too. Sorry, Windows users, MS-Schedule to too cumbersome for these quick uses and more like ‘cron’ – but still very heavy in comparison. There is an at.exe, but I get a permission denied on Win7 x64. Microsoft KB article on at.exe

I use at all the time to clean up temporary files a week later. So if a file is placed on a web server for a friend to grab, I email them the link and say the files will be automatically removed in 3 days. Right then, I schedule that removal with at and forget about it, certain it will be removed in 3 days.

Trackbacks

Use the following link to trackback from your own site:
https://blog.jdpfu.com/trackbacks?article_id=1009