Editor's note -- After reading the chapters Chris Stone contributed to Mac OS X: The Missing Manual, and previewing his work in Mac OS X in a Nutshell, I asked him to write a few articles for the Mac DevCenter to help people become more comfortable with the Terminal application in Jaguar (Mac OS X 10.2). These tutorials give you a preview of what Chris has covered in the Missing Manual.
If you've read some of his earlier tutorials, this series will feel vaguely familiar. That's because some of the examples here are the same as before. What's different is that this series is for Jaguar and the previous articles were written for Mac OS X 10 - 10.1. Jaguar changed a lot of things, and these new tutorials will show you how to work effectively in Apple's latest operating system.
Mac OS X's Terminal application -- there it sits in your Utilities folder, foreign and mysterious. You've heard that it's a portal to the new world of the Unix command line, a world where your flurries of mouse clicks can be replaced with a just few keystrokes.
But you've been wary of rushing into this new territory where the keyboard is king, concerned that without enough knowledge you might get lost, stuck, or worse. Or maybe you're an adventurer, just waiting to dive into uncharted waters.
This article is for you. Regardless of why you've previously avoided
[yourhost:~] yourname%, I'll show you how to take your first
steps with the Terminal application in Jaguar. Then I'll walk you through
a tutorial that will accelerate your understanding of the Unix command
In Part 1 of this series, you'll learn more about what Terminal does and get an overview of the tutorial procedure. In Part 2 you'll jump into the tutorial itself to learn the fundamental Unix commands necessary to get started with just about any command-line procedure.
Then, in Part 3, you'll finish the rest of the tutorial, as well as learn a few more things you can do with the command line.
The command-line interface (CLI) displayed in Terminal's windows provides access to the Unix shell, which is really just another way to interact with your Mac. The other method, which you're probably more comfortable with, is the Aqua interface. Aqua enables you to tell the Mac what to do by clicking on icons and menus to launch graphical applications.
The shell, on the other hand, allows you to type text commands to
accomplish much of the same work. Typically these typed commands launch
tiny, single-purpose Unix applications which do some specific work and
then quit. The shell itself is an application that plays the go-between
for the commands that you enter and the Unix kernel at the core of Mac OS
X. There are in fact several shells available. By default Mac OS X uses a
If you're curious about why you would want to use the shell in the first place, see the article Why Use a Command Line Instead of Windows? for more information about the CLI versus the Aqua interface.
To help you learn the Terminal application more quickly, I'm going to introduce you to a Unix utility built right into your Mac OS X system. Working with this utility will help you get more comfortable with core Unix commands.
Installed with Mac OS X is a mechanism that performs important
fine-tuning of your system. It's called
cron. By using this
Unix task-scheduling utility, your system can regularly purge itself of
outdated, space-hogging log files, update system databases so utilities
locate can work effectively, and do several other
maintenance tasks that keep your system running lean and mean.
cron utility fully automates this process, meaning
that once everything is configured, the housecleaning will happen
unattended as scheduled. The good news is that Apple has done the
configuration for you. The not-so-good news is that they've scheduled
these groups of tasks, or
cron jobs, to run between 4:00 and
5:00 in the morning -- a time when your Mac is likely not even on! And if
your Mac is never on during these times, these important tasks will never
happen. If your Mac is powered on but in deep sleep, the jobs still won't
In this tutorial, I'll show you first how to modify the
cron schedule, which is read from a file called the system
crontab, so that these tasks occur at more reasonable
times. I'll then explain how to configure Mac OS X's built-in mail server
so that you'll receive report by email every time the
For this tutorial, make sure you're running Mac OS X 10.2 or newer (for older versions of OS X, refer to earlier tutorials in this series), and that you're logged in with an administrator's, though not the root, account.
Open the Terminal program, which you'll find in the Applications --> Utilities folder. Once launched, Terminal opens a single window displaying the time of the last login, a greeting, and a third line of text that comprises the prompt. With that window active, anything you type will enter just before the rectangular cursor that follows the prompt. After you type a command, simply press Return or Enter to run it.
The prompt shows the name of your computer (or rather its host name, which can vary), and then identifies your current working directory ("directory" is just the Unix term for "folder"). The current working directory is "where you are," that is, the location in your filesystem hierarchy that your next command will act on. Your initial working directory is always your home directory, which is identified in the prompt by the home directory shortcut character "~".
To fully display the path to your working directory, use the
pwd command: Type
pwd (which means "print
working directory," though it only displays it) and press Return:
[haru:~] chris% pwd /Users/chris [haru:~] chris%
Chris is standing by if you want to chat about using the Terminal app in Jaguar.
As you can see,
pwd does its job by displaying the full
path, or pathname, to your home directory and providing you with a new
prompt when done. This path name begins with the slash character, which
represents the root or top-most directory of your filesystem. Note that
directories that reside on your system disk do not include that disk's
name in their pathnames.
To act on a different set of files, you simply change your working
directory using the
cd command. Our firs step is to modify
crontab file, which exists in the
directory (normally invisible to the Finder). Enter
followed by a space and the path name of the target directory,
[haru:/etc] chris% cd /private/etc [haru:/private/etc] chris% ls
Notice the change in the prompt reflecting the new working directory.
If you're curious about what your working directory contains, use the
ls, or list command:
[haru:/private/etc] chris% ls [haru:/etc] chris% ls 6to4.conf iftab rc.netboot afpovertcp.cfg inetd.conf resolv.conf appletalk.cfg kcpassword resolver appletalk.nvram.en0 kern_loader.conf rmtab atalk localtime rpc authorization magic rtadvd.conf bashrc mail services crontab mail.rc shells
As you can see, there are a lot of items -- quite a bit more than
what's shown here -- in
cron application launches automatically at system
startup and runs continuously in the background executing commands as
instructed by the
crontab files. These files tell
cron exactly what commands to run and when to run them. In
fact, each user account can have its own
crontab file. The
crontab found in
/private/etc belongs to
the super-user, or root account, and therefore can specify commands
requiring the same total system access allowed to root.
Before you modify the system
crontab, you should make a
backup copy in case you need to revert back to its default state. You'll
cp (copy) command to do this, which lets you copy and
rename a file in one step.
Normally to rename and copy a file into the
same directory, you would type
cp, followed by the name of
the original file, and then the name of the copy:
[haru: /private/etc] chris% cp crontab crontab.bak cp: crontab.bak: Permission denied
Hold on. It looks like you don't have permission to write to the
etc directory. In fact, only root can write to
/private/etc. Because you are not logged in as root, it seems
that there's no easy way to write to this directory. But there is.
sudo utility ("substitute-user do") allows you to gain
temporary root privileges on a per-command basis. To use
sudo, simply preface the command you wish to run as root with
sudo and a space, and
sudo will prompt you for
your password (not root's). If you have administrator privileges, entering
your password will run the
sudo'ed command as if the root
user were doing it.
sudo with care. You can easily make
sudo that could require a complete
re-installation of the OS to get going again. If that thought makes you
queasy, it would be wise for now to use
sudo only as directed
in this article.
To perform the previous command successfully, preface it with
[haru:/private/etc] chris% sudo cp crontab crontab.bak Password: [haru:/private/etc] chris%
sudo, you'll see another reminder to use
sudowithin the last 5 minutes.
Now that you have a backup, you'll want to know how to restore it should you make some kind of unrecoverable mistake when editing. The restore procedure is the reverse of the backup:
[haru:/private/etc] chris% sudo cp crontab.bak crontab
In this case, you're overwriting the non-working
file with a fresh copy of the original you've saved previously as
crontab.bak, which remains unchanged. Notice that, by
cp will overwrite a file without warning. If you
would instead like to be prompted to approve such overwrites, include
-i option flag.
Option flags allow you to modify the behavior of commands. The
-i option flag for
cp to display a prompt, asking you to allow the overwriting. Do so by typing
y for yes or cancel it by typing
n for no. To use the flag, simply type the
cp command, add a space, type the
-i flag, a space, and then the rest of the command:
[haru:/etc] chris% sudo cp -i crontab.bak crontab overwrite crontab? y [haru:/etc] chris%
What you need to do next, then, is edit this system
crontab file, and you'll learn how by using a command-line
text editor called
pico. However, if you were to first
examine the privileges for
/etc/crontab, you would see that
it's owned by root, and only root has write privileges. Sounds like
another job for
Of the several CLI text editors included with Mac OS X,
pico is the easiest to learn. To open a text file in
pico, simply enter the file name after the
command. Used with
sudo, the command to edit the
crontab file in the
/etc directory looks like
[haru:/private/etc] chris% sudo pico crontab
And this is what you'll see when you run it:
The document's text area lies between the black title bar at the top and the two rows of command prompts at the bottom. The Terminal window's scrollbar won't let you scroll through the document. Instead, you use the down-arrow to move the cursor down line by line, or use the Page commands.
All of the commands listed at the bottom are prefaced with the caret
character ("^"), representing the control key. So for example, to go to
the next "page" (or screenfull) of text, press the control and "V" keys as
indicated. For brief descriptions of all the commands, read the
pico help file by pressing control-G.
The numbers in the circled area specify the time
the scripts (there are actually three of them), and this is where you'll
make your changes.
Each of the three lines (numbered 1, 2, and 3) specifies one of the
cron runs by default. Each script is different,
performing its own appropriate set of maintenance procedures. The daily
script, specified on the line labeled 1, runs once each day. The weekly
script, specified on line 2, runs once each week. And the monthly script,
specified on line 3, runs -- you guessed it -- once each month.
The first five columns or fields of each line specify at exactly which interval the script will run. The fields specify, from left to right, the minute, hour (on a 24-hour clock), day of the month, month, and weekday (numerically, with Sunday as 7). Asterisks used instead of numbers in these fields mean "every".
For example, line 1 specifies a time of 3:15 a.m.:
15 3 * * * root periodic daily
Since the rest of the columns contain asterisks, the daily script will run at "3:15 a.m. on every day of the month, on every month, and every day of the week," that is "every day at 3:15 a.m.".
Line 2 specifies that the weekly script runs at 4:30 a.m. on every weekday number 6, or Saturday:
30 4 * * 6 root periodic weekly
And line 3 specifies that the monthly script runs at 5:30 a.m. on day 1 (the first) of each month.
30 5 1 * * root periodic monthly
By just changing these numbers, then, you can have these scripts run at more reasonable times. Of course, what counts as reasonable depends on your own situation, so consider these factors when deciding:
For example, these times might be good for a machine that's only on during normal work hours:
Regarding the monthly job, the first of the month sometimes falls on a weekend or holiday, but for now that's the best you can do.
To modify the
crontab file to reflect these new times,
use the cursor keys (the four arrow keys) to move the cursor to the proper
field. Except for being unable to use the mouse, you'll find that editing
pico is similar to doing so with any GUI text
editor. Use the delete key as usual, and type in the new values.
First, change the 3 in the daily script line to 17:
15 17 * * * root periodic daily
Next, change the time in the weekly script line as shown; change the day from 6 to 2 (Saturday to Tuesday).
50 8 * * 2 root periodic weekly
Finally, change the time in the monthly script line as shown:
30 9 1 * * root periodic monthly
Once you've made the changes, save ("write out") the document by pressing control-O. You'll then be prompted to confirm the save. Just press Return to do so.
pico, by pressing control-X.
Once you've saved the
crontab file, the new scheduling
takes effect; there's no need to restart. You won't yet receive
notification of the completed
cron jobs, but in Part 2,
you'll learn how to make that happen, as well as learn more about the
Chris Stone is a Senior Macintosh Systems Administrator for O'Reilly, coauthor of Mac OS X in a Nutshell and contributing author to Mac OS X: The Missing Manual, which provides over 40 pages about the Mac OS X Terminal.
Read more Learning the Mac OS X Terminal columns.
Return to the Mac DevCenter.
Copyright © 2009 O'Reilly Media, Inc.