Learning the Mac OS X Terminal: Part 1
Pages: 1, 2
cp
Before you modify the system crontab, you should first make a backup copy in case you need to revert back to its default state. You’ll use the cp, or 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:
[crontab: /private/etc] chris% cp crontab crontab.bak
cp: crontab.bak: Permission denied
But 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. So, because you are not logged in as root, it might seem that there’s no easy way to write to this directory. But there is....
sudo
The sudo utility, for "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 root were doing it.
Warning: Use sudo with care. You can easily make mistakes with sudo that could require 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 sudo:
[office_g4:/private/etc] chris% sudo cp crontab crontab.bak
Password:
[office_g4:/private/etc] chris%
Notes about sudo:
- The first time you run
sudo, you’ll see another reminder to usesudowith care. - You’ll only need to enter your password when you haven’t already used
sudowithin the last 5 minutes. - It’s not necessary to activate the root account to use
sudo.
What you need to do next, then, is edit this system crontab file, and you’ll learn how to do it with 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 sudo!
pico
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 pico command. Used with sudo then, the command to edit the crontab file in the /etc directory looks like this:
[localhost:/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" (actually screen-full) 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 cron runs 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 three scripts 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 what interval the script will run. The fields specify from left to right: 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 sh /etc/daily 2>&1 | tee /var$ …
Since the rest of the columns contain asterisks, the daily script (which is written in a file named on that line by its path name /etc/daily) 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 sh /etc/weekly 2>&1 | tee /var$ …
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 sh /etc/monthly 2>&1 | tee /var$ …
By just changing these numbers, then, you can have these scripts run at more reasonable times. Of course, what’s "reasonable" depends on your own situation, so consider these factors when deciding:
- Choose a time when your Mac is likely to be on (and not asleep).
- Choose a time when a few minutes of background activity won’t disturb your work too much. On faster machines especially, the activity is hardly noticeable, but it could cause some stuttering if, for example, you happened to be watching a DVD at the time.
- Choose a time that is unique for each script. You don’t want to schedule scripts to run at the same time.
For example, these times might be good for a machine that’s only on during normal work hours:
- Daily -- every day at 5:15 p.m.
- Weekly -- every Monday at 8:50 a.m.
- Monthly -- the first of every month at 9:30 a.m.*
*(Of course, the first of the month sometimes falls on a weekend or holiday, but for now, that’s the best you can do. You’ll find a work around to this problem in Part 2 of the article.)
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 text with 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 sh /etc/daily 2>&1 | tee /var$ …
Next, change the time in the weekly script line as shown, and the day from 6 to 2 (Saturday to Tuesday).
50 8 * * 2 root sh /etc/weekly 2>&1 | tee /var$ …
Finally, change the time in the monthly script line as shown:
30 9 1 * * root sh /etc/monthly 2>&1 | tee /var$ …
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.

Finally, quit pico, by pressing control-X.
Once you’ve saved the crontab file, the new scheduling takes effect; there’s no need to restart. For now, you’ll not 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 scripts themselves.
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.
O'Reilly & Associates recently released (December 2001) Mac OS X: The Missing Manual.
Sample Chapter 2, Organizing Your Stuff, is available free online in PDF format.
You can also look at the Full Description of the book.
-
For more information, or to order the book, click here.
Return to the Mac DevCenter.
-
NOPE - I don't get it.
2010-02-28 09:18:33 zeroge [View]
-
NOPE - I don't get it.
2010-03-10 16:14:18 joel1138 [View]
-
Sudo code not working "command not found"
2009-08-26 06:47:21 renrut16 [View]
-
no such file or directory
2009-05-05 05:35:40 Carson Ramsdell [View]
-
Please note: Author's note at top of page 1 about applicable OSes
2008-02-14 15:13:59 lpainter [View]
-
Why do images change to terminal icon?
2007-12-18 12:42:04 Jynja [View]
-
USING TERMINAL FOR MAIL
2007-11-30 11:04:25 elcid07 [View]
-
when i do the "sudo pico crontab" thing, it doesn't work
2007-08-30 14:48:29 uber_noob [View]
-
Oasis members really did it!!!
2007-08-08 03:43:37 alexeiw [View]
-
hello
2007-08-08 03:42:34 alexeiw [View]
-
Today, for the first time in the 25-year history
2007-08-08 03:41:12 alexeiw [View]
-
Sudo command...
2007-05-09 20:10:33 DKAMagic [View]
-
So far, so good
2007-02-12 13:59:54 Harlemite [View]
-
Command line for Word
2006-01-27 04:53:10 Charliefarley [View]
-
Command line for Word
2006-11-29 09:32:16 tcomputerchip [View]
-
sudo pico crontab
2006-01-23 11:05:23 jrodri26 [View]
-
sudo pico crontab
2008-10-19 09:44:27 voice18 [View]
-
sudo pico crontab
2006-01-23 12:22:35 Chris Stone |
[View]
-
Perl Scripts
2006-01-09 14:38:01 Cruzapete [View]
-
Perl Scripts
2006-11-29 09:39:26 tcomputerchip [View]
-
Process cron stealing a lot of cpu time
2005-11-15 10:31:58 Vilhs [View]
-
How do I manage different hard drives?
2004-10-26 12:26:29 Cissi [View]
-
How do I manage different hard drives?
2005-10-14 10:20:53 js2000 [View]
-
Mail in OS X Terminal?
2004-05-01 18:47:20 surfs [View]
-
Mail in OS X Terminal?
2004-05-02 15:54:12 lpb2h [View]
-
I feel like a techy already
2004-03-31 03:46:47 10n6 [View]
- Trackback from http://www.joeworld.net/mt/archives/000022.html
Learning the Mac OS X Terminal
2003-11-06 04:15:54 [View]
-
sudo pico crontab
2003-08-19 21:35:30 anonymous2 [View]
-
Toad
2003-08-09 15:35:12 anonymous2 [View]
-
coachferrigan
2003-07-09 14:04:56 anonymous2 [View]
-
WriteOut?
2003-07-06 00:52:12 anonymous2 [View]
-
Connecting to a remote server?
2003-06-25 17:00:35 anonymous2 [View]
-
Connecting to a remote server?
2004-01-13 12:10:01 anonymous2 [View]
-
pico error
2003-06-11 22:48:24 anonymous2 [View]
-
Learned alot
2003-03-16 11:47:06 buz [View]
-
cd and directory names
2003-03-11 19:20:03 anonymous2 [View]
-
cd and directory names
2004-03-27 20:47:22 rcrow [View]
-
its easy
2003-03-12 07:36:38 anonymous2 [View]
-
its easy
2003-03-12 09:26:20 Chris Stone |
[View]
-
its easy - and thanks
2003-03-13 05:20:15 macmartin [View]
-
New User Account!?!?!!?
2003-03-05 18:13:57 anonymous2 [View]
-
New User Account!?!?!!?
2003-03-05 18:12:50 anonymous2 [View]
-
UNIX CLI
2003-02-24 09:21:56 anonymous2 [View]
-
CronniX
2003-02-08 17:50:21 anonymous2 [View]
-
Trying the command line for the first time
2003-01-20 13:46:58 anonymous2 [View]
-
Can't enter my password!
2003-01-16 19:25:28 anonymous2 [View]
-
Can't enter my password!
2003-01-17 08:47:30 Chris Stone |
[View]
-
Annoying Programs
2002-12-17 08:40:07 anonymous2 [View]
-
Annoying Programs
2003-11-19 16:06:48 anonymous2 [View]
-
Suggestions for a Jaguar User who has erred?
2002-10-21 09:27:46 bebo [View]
-
Very Interesting...
2002-09-06 21:45:16 zeta74 [View]
-
Very Interesting...
2002-09-30 08:53:34 anonymous2 [View]
-
/private/etc/crontab in Jaguar
2002-09-02 20:05:38 mike_dowe [View]
-
cron jobs
2002-08-15 19:49:03 monica1 [View]
-
cron jobs
2002-09-30 08:59:02 anonymous2 [View]
-
problems with sudo????
2002-08-08 02:52:52 rlois [View]
-
problems with sudo????
2003-06-01 07:38:33 anonymous2 [View]
-
problems with sudo????
2003-02-13 04:45:58 anonymous2 [View]
-
Different PICO output?
2002-08-07 18:24:42 mepasha [View]
-
Different PICO output addition:
2002-08-07 18:27:47 mepasha [View]
-
Pico ?
2002-07-09 02:52:11 phil_roy [View]
-
Pico ?
2003-07-20 23:41:32 anonymous2 [View]
-
Terminal to finder and back
2002-06-02 20:53:37 michal.young [View]
-
Terminal to finder and back
2010-08-15 03:35:53 pklaus [View]
-
Terminal to finder and back
2002-07-03 07:51:15 gasoline [View]
-
/etc vs. /private/etc?
2002-04-02 10:40:29 lorenrf [View]
-
/etc vs. /private/etc?
2002-04-04 10:19:39 Chris Stone |
[View]
-
/etc vs. /private/etc?
2008-01-22 02:05:44 frank143 [View]
-
Error
2002-03-14 04:19:10 sevenov9 [View]
-
Even nitpickier
2002-03-13 12:12:02 socokid [View]
-
Initial Prompt
2002-03-05 12:37:50 mgoins11 [View]
-
Initial Prompt
2003-06-03 05:19:23 anonymous2 [View]
-
Being Careful
2002-03-01 14:05:20 kwidholm [View]
-
Being Careful
2003-11-02 00:14:29 anonymous2 [View]
-
Macs & OS X: A beautiful combo
2002-02-10 16:37:05 dasper [View]
-
Do cron jobs run even if Mac is sleeping?
2002-01-31 10:22:00 nolanl [View]
-
Do cron jobs run even if Mac is sleeping?
2003-05-14 09:56:17 anonymous2 [View]
-
Do cron jobs run even if Mac is sleeping?
2002-02-20 16:37:21 Chris Stone |
[View]
-
Do cron jobs run even if Mac is sleeping?
2002-02-01 12:17:26 Chris Stone |
[View]
-
Groups: wheel and staff
2002-01-29 12:17:53 pattyb777 [View]
-
Groups: wheel and staff
2002-02-07 05:26:52 xa40d [View]
-
Part II please!
2002-01-16 11:02:21 tripphome [View]
-
Part II please!
2002-01-17 11:07:37 Chris Stone |
[View]
-
/etc/hosts Not Used?
2002-01-02 19:12:59 ellem [View]
-
/etc/hosts Not Used?
2002-01-04 13:23:05 Chris Stone |
[View]
-
behavior of cc in OS X terminal window
2001-12-21 17:03:42 pistor [View]
-
behavior of cc in OS X terminal window
2001-12-21 20:24:42 hokanstenholm [View]
-
crontab -e
2001-12-21 09:54:12 sharumpe [View]
-
crontab -e
2003-03-10 16:03:06 anonymous2 [View]
-
crontab -e
2003-07-15 11:10:50 anonymous2 [View]
-
crontab -e
2001-12-21 10:52:44 jope [View]
-
crontab -e
2001-12-21 11:48:47 Chris Stone |
[View]
-
"pico -w" for editing config files
2001-12-21 07:42:20 Jerry Peek |
[View]
-
"pico -w" for editing config files
2001-12-21 09:07:21 Chris Stone |
[View]
-
excellent article, small typo
2001-12-20 15:20:58 sootmann [View]
-
PWD
2001-12-20 15:07:48 altp [View]
-
PWD
2001-12-20 17:56:53 Chris Stone |
[View]
-
anacron?
2001-12-19 10:15:03 jope [View]
-
anacron?
2001-12-21 11:02:57 jope [View]
-
anacron?
2001-12-21 14:42:39 Chris Stone |
[View]
-
Mac-like editing
2001-12-18 09:50:45 canyonrat [View]
-
Mac-like editing
2001-12-18 10:33:41 Chris Stone |
[View]
-
A little nitpick
2001-12-18 06:16:46 pmccann [View]
-
A little nitpick
2001-12-18 09:09:35 Chris Stone |
[View]
-
A little nitpick
2001-12-18 12:35:52 Chris Stone |
[View]
-
Difference between OS X crontab and Unix crontab
2001-12-17 15:57:44 drew_in_japan [View]
-
Difference between OS X crontab and Unix crontab
2001-12-17 18:00:13 Chris Stone |
[View]
-
Difference between OS X crontab and Unix crontab
2003-12-02 22:07:56 anonymous2 [View]

