Use at and batch to automate routine tasks

Jan 22, '03 09:55:53AM

Contributed by: GEllenburg

Since Mac OS X runs on top of Darwin, which is based on FreeBSD, we have a plethora of utilities available to us to help us automate our Macs. If you saw the hint about disabling the root password for sudo, then the following hints should make your life just a little bit easier (if you use fink, especially). Note that you may need the BSD subsystem packages installed in order to use this hint. I'm not sure, since I already have those packages installed.

You may or may not be familiar with cron, a daemon which allows you to schedule repetitive tasks to run throughout the day. However, there are two additional commands, not widely used, which can really make your life just a little bit easier. They are, at and batch. at and batch are really the same commands, but do different things depending on how they're called.

at allows you to schedule tasks, or an application, to execute at a specific date and time in the future. Unlike cron, which will run the application every n minutes, hours, days, weeks, etc., at will only run a command once. I use it all the time for reminders, like:

 % echo "mail -s 'REMINDER: Feed the dog' myname@myemail.com" | at 18:30
This will send me an email at 6:30pm to remind me to feed my dog. You can do a man at to see its manual and learn additional things you can do.

Another command is called batch. batch is neat in that you can schedule a job to run, almost immediately. It will quietly run in the background, doing its thing. My NetInfo aliases database, and my .forward in my home-directory, automatically forward any mail sent to my local Mac account (both at and batch will generate an Email output when they're done so you can see what happened) are forwarded to my main Email address for inspection. For example, let's say I'm getting ready to go to work, but I want to have fink update everything. I can simply type:
 % batch 
... which will show nothing but a cursor. batch is waiting for me to type whatever commands I want it to do. Now, all I need to enter is:
 % sudo fink -y update-all ^D
The ^D at the end is a Control+D, and is important to tell batch that you're done entering the commands.

As soon as you type ^D, within the next five minutes, the command will run. I can safely close all applications and even log out of my Mac if I want to (but not shut it down!). When I get home at the end of the day, there will be an email waiting for me with the output of everything which would have been sent to the screen. But, in order to use these commands, you first need to enable them. OS X ships with the "at" scheduler disabled by default. Here's how to enable the at-scheduler. In the Terminal, type:
 % sudo pico /etc/crontab
Remove the hash-mark (#) from the line which reads:
 #*/5     *       *       *       *       root    /usr/libexec/atrun
So it should now read:
 */5     *       *       *       *       root    /usr/libexec/atrun
Then exit and save your changes. That's it!

Some suggested uses for `at` and `batch`:The possibilities are endless...

Comments (18)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20030122065553454