Two Stops Down

A journal on photography by Thomas Riggs

Running Scripts In The OS X Terminal (From The Command Line)

One of these days I’ll make a write-up about my own personal backup solution, but today I came across an interesting command in the OS X terminal that should be of benefit to a lot of tech-savvy Mac users.

For my personal backups, I have a shell script I run daily to take care of everything. The script outputs text as it does its backup, so that once it’s done, I can see if it had any problems. This works great, until you start to use cron jobs.

Cron jobs are a powerful part of a UNIX system, similar to Tasks in Windows. The idea is that you can set a certain terminal command to run at a specific time, at certain intervals, in numeric ranges, and all sorts. To me, this seems like a great way to automate my backup system.

The problem is, cron jobs are part of the system’s background processing. You don’t see it. So, if I set up my backup script to run, say, at three in the morning, I have no clue if it ran or not without checking my log files. Ideally what I would want is to open up the Terminal.app, and have that run the script, rather than cron.

So how do you do this? With the open command.

open is useful for lots of jobs in the terminal. All it does is it opens the specified file in the default application, as chosen by Mac OS X. For example, a command like this:

> open ~/todo.txt

would open my todo.txt file in the default text editor, such as TextEdit or TextMate.

But here’s the ninja move you can use the -a flag to specify the application. So, if I want to open up my backup script in the terminal, I tell cron to run this command:

> open -a /Applications/Utilities/Terminal.app ~/backup.sh

So now, rather than running silently in the background, an instance of Terminal.app pops open, and starts running backup.sh. Just what I wanted. Thanks OS X!

This article was published on 7 April 2009.