Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Use at and batch to automate routine tasks UNIX
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`:
  • echo "mail -s 'DENTIST APPOINTMENT AT 2PM' you@yourdomain.com" | at '4/23/2003 10:00AM' - Mail yourself a reminder for upcoming dentist appointments. The single and double-quotes are required because of the spaces.

  • echo "sudo fink -y selfupdate-cvs" | batch - Update fink, within the next five minutes, in the background.

  • echo "sudo fink -y install bundle-kde-ssl" | at midnight - At midnight tonight, have fink install KDE for you, while you're asleep!
The possibilities are endless...
    •    
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[10,919 views]  

Use at and batch to automate routine tasks | 18 comments | Create New Account
Click here to return to the 'Use at and batch to automate routine tasks' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Can't run at
Authored by: rjbailey on Jan 22, '03 12:41:29PM

I can't run 'at':

[Hostname:~] jeff% echo "This is a test" | at 09:38
at.deny: No such file or directory
You do not have permission to use at.
[Hostname:~] jeff%

I entered myself in /var/cron/allow, but this didn't help.
Mac OS X 10.2.3



[ Reply to This | # ]
Man has answers
Authored by: nick2588 on Feb 18, '03 07:11:23PM
Stated in the man pages, it says that you cannot use /var/at/at.allow. Read the man pages (or instructions, or directions, or readme, or whatever documentation you have) before asking questions. This would be a good practice for all of us.

    BUGS

    Traditional access control to at and batch via the files /var/at/at.allow and /var/at/at.deny is not implemented.


[ Reply to This | # ]
Man has answers
Authored by: yosithezet on Feb 19, '03 01:05:15AM

I saw that, but what does it mean? Users can't use at and batch? Must
sudo?



[ Reply to This | # ]
Man has answers
Authored by: rjbailey on Mar 11, '03 08:26:15PM
Stated in the man pages, it says that you cannot use /var/at/at.allow. Read the man pages (or instructions, or directions, or readme, or whatever documentation you have) before asking questions. This would be a good practice for all of us.
I did RTFM, but the only thing that allowed me to use 'at' was to add myself to /var/at/at.allow, your not-so-helpful post notwithstanding. I also needed to add a directory /var/at/spool before 'at' jobs would run.

[ Reply to This | # ]
Figured that one out...
Authored by: rjbailey on Jan 22, '03 01:00:13PM

I added myself to /var/at/at.allow, and that takes care of the previous error.



[ Reply to This | # ]
Difference between batch and &
Authored by: triplef on Jan 22, '03 06:31:54PM

Is there any difference between using batch and using the "&" postfix in bash?
I would do it like this:
sudo fink -y update-all &
... and the command will happily run in the background.

Maybe I'm missing something.



[ Reply to This | # ]
Difference between batch and &
Authored by: Tiresias on Jan 22, '03 09:38:25PM

I haven't used unix stuff for long enough so I don't know but I think that when you use & you still own it. If I read it correctly, this method runs things as root which means your user doesn't have to be logged in.



[ Reply to This | # ]
Difference between batch and &
Authored by: sardu_mac on Jan 23, '03 06:55:16AM
It doesn't run the command 'as root' but rather the userid that executed the command. It also retains things like environment variables and current working directory

From the manpage:
For both at and batch, the working directory, environment (except for the
variables TERM, TERMCAP, DISPLAY, and _) and the umask are retained from
the time of invocation. The user will be mailed the standard output and
standard error from his commands if any output is generated. If at is
executed from a su(1) shell, the owner of the login shell will receive
the mail.


[ Reply to This | # ]
Difference between batch and &
Authored by: GEllenburg on Jan 23, '03 07:01:53AM

The `batch` command does one other important thing which using the ampersand operater can not...

One can queue up tens, or even hundreds of commands to run "... in batch ...," but if the load average on the machine is too high, batch will wait for the load to drop down before executing further commands so the system doesn't become unstable or unusable.

Simply placing a command in the background (using the ampersand) can't do this.



[ Reply to This | # ]
Difference between batch and &
Authored by: sardu_mac on Jan 23, '03 06:58:58AM

Using & executes right away, rather than, say, updating fink at 3:00AM when you won't miss the extra CPU cycles.



[ Reply to This | # ]
mp3 alarmclock?
Authored by: Tiresias on Jan 22, '03 09:47:07PM

Anyone know how to implement this as an MP3 alarmclock? I've got mpg123 (commandline mp3 player) which I would like to use as an alarm in the morning but I haven't gotten it to work. My other option is to make it run "open musicfile.mp3" which will automatically open in iTunes due to the nature of open. This method isn't acceptable because I can't get a random mp3 to play or a list of mp3s to play like I could do with mpg123.



[ Reply to This | # ]
mp3 alarmclock?
Authored by: tcurtin on Jan 22, '03 11:38:17PM

Check out this previous article here - the poster included a bash script that will run itunes and tell it to play from a playlist. You can pretty easily adapt what you get from the script to your own nefarious purposes. ;)

http://www.macosxhints.com/article.php?story=20020110163623707

For instance, I put the following line (all one line) into my .cshrc file in my home directory:
alias music 'echo tell application \"Itunes\" to play \(some track of playlist \"Library\"\) | /usr/bin/osascript; echo tell application \"Finder\" to set visible of process \"itunes\" to false | /usr/bin/osascript'

Once you add that line, quit Terminal, and when you restart it you can type 'music' in any terminal window, and itunes starts up, plays a random track, and hides itself. I'm used to writing scripts in perl and sh - AppleScript is taking a while to get used to, but this makes a good start!

Good luck,
-Tim



[ Reply to This | # ]
mp3 alarmclock?
Authored by: GEllenburg on Jan 23, '03 07:21:45AM

Yes,

I do this all the time. Note though, my iTunes folder is not in my Library folder, it's on an NFS share on my file-server, so you'll need to modify the path below to reference where YOUR iTunes folder is:

% at 7:10<Return>
/sw/bin/mpg123 -a CoreAudio -os "/mnt/athlon/stuff/music/Soundtrack/The Sopranos/01 A3 - Woke Up This Morning.mp3"
^D

This will play the theme song to The Soprano's at 7:10am.

Of course, one could do the same by adding a line to their crontab by typing:

% crontab -e<Return>

...

0 5 * * * /sw/bin/mpg123 -a CoreAudio -os "/mnt/athlon/stuff/music/Soundtrack/The Sopranos/01 A3 - Woke Up This Morning.mp3"


Hope this helps.



[ Reply to This | # ]
Anacron
Authored by: bluehz on Jan 23, '03 08:47:13AM

Whats the difference between at, batch, and anacron? For some reason I have anacron running and it seems like it is at related but not sure...



[ Reply to This | # ]
Anacron
Authored by: sardu_mac on Jan 24, '03 04:54:18AM

> Whats the difference between at, batch, and anacron?

'at' is a command queue system for running commands 'at' a specific time.

'batch' is also a command queue but the commands you submit to batch run when the system load is below a certain level rather than at a specified time.

'anacron' is for repeating tasks, similar to the traditional cron daemon on UNIX systems, however anacron doesn't assume your machine is up 24/7 and allows you to segregate anacron jobs into daily/weekly/monthly groups. If your anacron was installed via Fink you can see it's "tab file" using 'cat /sw/etc/anacrontab'.

Pop open Terminal and breeze through the manpages (ie. 'man anacrontab') for more info.



[ Reply to This | # ]
Using
Authored by: Andrea on Feb 05, '03 09:41:04AM

Can I use "at" to run a "configure-make-make install" dance? I mean, if I write a file (say runit.txt) with the following commands:

./configure
make
make install

can I get the wanted result with:

% at -f runit.txt 00:00am

???



[ Reply to This | # ]
Use at and batch to automate routine tasks
Authored by: yosithezet on Feb 18, '03 10:58:18AM
Created /var/at/at.allow
-rw-r--r--    1 root     wheel           6 Feb 17 23:07 at.allow

Added some

at
and
batch
jobs:
[108][david@localhost-5:53pm]/Installs] > atq

Date                    Owner   Queue   Job#

23:10:00 02/17/03       david   a       a0109e376.000

23:14:00 02/17/03       david   b       b0109e37a.000

23:17:00 02/17/03       david   b       b0109e37d.000

But they didn't run. Any ideas?

[ Reply to This | # ]

Use at and batch to automate routine tasks
Authored by: yosithezet on Feb 18, '03 10:58:21AM
Created /var/at/at.allow
-rw-r--r--    1 root     wheel           6 Feb 17 23:07 at.allow

Added some

at
and
batch
jobs:
[108][david@localhost-5:53pm]/Installs] > atq

Date                    Owner   Queue   Job#

23:10:00 02/17/03       david   a       a0109e376.000

23:14:00 02/17/03       david   b       b0109e37a.000

23:17:00 02/17/03       david   b       b0109e37d.000

But they didn't run. Any ideas?

[ Reply to This | # ]