Learning the Terminal in Jaguar, Part 1
Pages: 1, 2
cp
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
use the 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
The 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.
Warning: Use sudo with care. You can easily make
mistakes with 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
sudo:
[haru:/private/etc] chris% sudo cp crontab crontab.bak
Password:
[haru:/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.
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 crontab
file with a fresh copy of the original you've saved previously as
crontab.bak, which remains unchanged. Notice that, by
default, cp will overwrite a file without warning. If you
would instead like to be prompted to approve such overwrites, include
cp's -i option flag.
Option flags allow you to modify the behavior of commands. The -i option flag for cp tells 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 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, the command to edit the
crontab file in the /etc directory looks like
this:
[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 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 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:
- 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 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.
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
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 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.

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. 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
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.
Read more Learning the Mac OS X Terminal columns.
Return to the Mac DevCenter.
-
"crontab" does not exist
2008-11-27 20:03:47 Nora O'Neil [View]
-
vanila.r
2008-10-12 15:24:43 NMEfoofoo [View]
-
vanila.r
2008-10-12 15:22:55 NMEfoofoo [View]
-
cd/private/etc does not work
2008-05-14 08:09:00 beata2 [View]
-
cd/private/etc does not work
2008-05-14 08:52:38 Chris Stone |
[View]
-
help
2008-02-27 18:19:51 duß#8706; unverified 349845 [View]
-
tcsh: 1s: Command not found.
2007-04-03 20:15:05 andresoueu [View]
-
tcsh: 1s: Command not found.
2007-04-03 20:19:16 andresoueu [View]
-
downloads
2007-02-22 16:36:14 soookinky [View]
-
Cannot edit/find "crontab" to edit
2007-02-22 08:46:42 rupertreid [View]
-
Cannot edit/find "crontab" to edit
2007-07-10 16:00:07 John! [View]
-
Cannot edit/find "crontab" to edit
2007-07-10 16:04:02 John! [View]
-
Creating invisable files/folders using terminal
2006-07-24 11:59:40 Billymac2 [View]
-
Command Line PHP Version?????
2006-05-28 09:00:53 VectorSalad [View]
-
not executable
2006-04-16 17:05:36 busdriverneal [View]
-
Great!
2006-03-18 17:39:46 mmsomekid [View]
-
Thanks!
2006-03-18 17:39:38 mmsomekid [View]
-
Creating encrypted passwords for htpasswd
2006-02-10 09:34:27 buzcajun [View]
-
immediate problems
2006-02-06 07:44:38 Granger [View]
-
immediate problems
2006-02-06 08:02:31 Chris Stone |
[View]
-
Wont let type password
2006-01-13 16:00:26 Skindc [View]
-
Wont let type password
2006-02-06 08:05:27 Chris Stone |
[View]
-
crontab moved
2005-08-24 14:02:29 WinterLavigne [View]
-
crontab moved
2007-08-14 02:23:28 Semiotikus [View]
-
crontab moved
2005-09-07 01:41:04 andrewf [View]
-
Finding A Hidden Folder
2005-06-15 15:34:48 immisumord [View]
-
Finding A Hidden Folder
2007-08-28 18:07:14 Jonny81 [View]
-
crontab: No such file or directory
2005-02-04 06:21:14 Whitmor [View]
-
crontab: No such file or directory
2005-03-04 02:38:10 turver [View]
-
compiling c with terminal
2005-01-24 18:40:52 johnwoodruff [View]
-
compiling c with terminal
2007-04-30 00:34:18 jdkorreck [View]
-
How can I cancel a command?
2005-01-06 07:36:22 juf [View]
-
Safari
2005-01-04 17:59:23 yizzle123 [View]
-
Using terminal to "see" a web request
2005-06-07 14:24:23 xurizaemon [View]
-
Safari
2005-06-07 14:18:59 xurizaemon [View]
-
Safari
2005-02-10 11:39:18 endian [View]
-
Commands not found in Terminal
2004-12-21 10:13:12 jamiekravitz [View]
-
Running a simple C program
2004-12-16 22:39:20 JoshO [View]
-
Terminal window in micro size
2004-11-18 17:46:14 Sinchee [View]
-
Terminal window in micro size
2005-05-26 19:36:23 FionaCanning [View]
-
can't use cp or mv commands!
2004-05-28 13:18:27 macmanjws@earthlink.net [View]
-
Logging into CVS through Terminal
2004-03-02 13:41:42 dmc [View]
-
Logging into CVS through Terminal
2004-05-06 12:21:58 CaptainMikee [View]
-
disk utility at the command line
2004-02-17 11:37:15 bobbyb [View]
- Trackback from http://www.osxx.org/macintosh/000004.php
Unleash the power of UNIX in your OS X box
2004-02-05 23:01:55 [View]
-
fix Filemaker with script
2004-01-21 11:19:49 rf10 [View]
-
fix Filemaker with script
2004-02-01 19:53:25 dhrowe [View]
-
Won't let me telnet
2004-01-10 23:33:51 anonymous2 [View]
-
Won't let me telnet
2004-04-16 10:37:06 Saradhi [View]
-
Is there or will ther be an update for 10.3.2?
2004-01-06 17:12:11 macnut [View]
-
maintenance script vs macjanitor
2003-12-29 19:14:19 anonymous2 [View]
-
maintenance script vs macjanitor
2004-01-11 07:26:33 anonymous2 [View]
-
Taming the Terrifying Terminal
2003-12-03 17:58:53 anonymous2 [View]
-
Terminal "hijacked"
2003-11-12 15:42:05 anonymous2 [View]
-
Help-Can I run this in Terminal ?
2003-11-04 09:30:28 anonymous2 [View]
-
how do i type in a command, comp keeps repeating my user name and saying command not found
2003-10-27 08:32:02 anonymous2 [View]
-
Crontab running time??
2003-10-24 13:44:22 anonymous2 [View]
-
after changing my cleanup schedule
2003-10-09 16:47:00 anonymous2 [View]
-
after changing my cleanup schedule
2003-10-15 01:57:57 anonymous2 [View]
-
best terminal tutorial
2003-10-09 11:30:08 anonymous2 [View]
-
The mechanics of tcsh & csh
2003-09-20 09:27:06 anonymous2 [View]
-
Terminal Not Working
2003-09-13 11:16:51 tfanning [View]
-
De-Fanged Terminal
2003-09-12 00:08:05 empass [View]
-
nowhere man
2003-08-21 21:17:40 anonymous2 [View]
-
message warning
2003-08-12 07:59:08 anonymous2 [View]
-
cron
2003-07-10 02:46:00 anonymous2 [View]
-
Great stuff
2003-05-19 02:05:52 anonymous2 [View]
-
Screenshots in JPEG
2003-05-15 13:23:18 anonymous2 [View]
-
Screenshots in JPEG
2003-05-20 02:36:40 anonymous2 [View]
-
deleting large numbers of files
2003-05-02 16:32:48 anonymous2 [View]
-
deleting large numbers of files
2003-06-03 15:48:58 anonymous2 [View]
-
HELP with pico
2003-04-16 22:29:56 cpbjr [View]
-
HELP with pico
2005-03-18 14:47:41 suhuba [View]
-
I had the same result.
2003-04-23 20:31:12 anonymous2 [View]
-
I had the same result.
2003-04-24 08:50:13 Chris Stone |
[View]
-
1s does not work??
2003-03-28 00:03:08 anonymous2 [View]
-
Kudos
2003-03-21 18:20:48 anonymous2 [View]
-
Learning Jag Terminal
2003-03-16 10:15:54 buz [View]
-
Learning Jag Terminal
2003-08-20 18:09:27 anonymous2 [View]
-
Learning Jag Terminal
2003-06-12 16:29:24 anonymous2 [View]
-
Learning Jag Terminal
2003-05-14 04:21:45 anonymous2 [View]
-
cannot open file for writing
2003-03-01 08:57:49 anonymous2 [View]
-
- of course! 'sudo'
2003-03-01 09:33:21 riquez [View]
-
JDBC Data Sources
2003-02-09 22:47:45 anonymous2 [View]
-
mail gives no error, yet recipient never gets
2003-02-06 15:44:50 anonymous2 [View]
-
Terminal to access remote server
2003-02-05 20:05:47 anonymous2 [View]
-
Terminal to access remote server
2003-02-05 20:05:07 anonymous2 [View]
-
I can not find periodic
2003-01-29 00:04:10 anonymous2 [View]
-
I can not find periodic
2003-01-31 09:44:49 Chris Stone |
[View]
-
sudo: pico: command not found
2003-01-28 11:32:51 alphaville [View]
-
sudo: pico: command not found
2003-01-28 11:47:29 Chris Stone |
[View]
-
sudo: pico: command not found
2003-01-28 12:24:33 alphaville [View]
-
Pilot error & Pico help.
2003-01-27 10:40:41 anonymous2 [View]
-
Pilot error & Pico help.
2003-01-28 11:43:27 Chris Stone |
[View]
-
what is sudo?
2003-01-24 14:51:35 anonymous2 [View]
-
terminal usage for apache?
2003-01-23 14:04:04 anonymous2 [View]
-
terminal usage for apache?
2003-01-23 14:25:58 Chris Stone |
[View]
-
shutdown and restart
2003-01-19 07:54:37 anonymous2 [View]
-
shutdown and restart
2003-12-11 08:31:39 anonymous2 [View]
-
installed Fink, now terminal starts in 'dselect'
2003-01-14 11:44:51 anonymous2 [View]
-
tcsh or bash
2002-12-24 00:19:55 anonymous2 [View]
-
Korn shell
2002-12-24 00:13:47 anonymous2 [View]
-
Korn shell
2004-03-19 11:30:18 mozart467 [View]
-
Korn shell under OS X?
2002-12-23 07:46:36 anonymous2 [View]
-
crontab under /etc and /private/etc
2002-12-22 15:21:38 anonymous2 [View]
-
Missing features of 10.2 ???
2002-12-22 08:51:17 anonymous2 [View]
-
Help Personalizing the CLI?
2002-12-21 16:36:20 anonymous2 [View]
-
tcsh or bash?
2002-12-21 11:13:26 anonymous2 [View]
-
Apple Laptop Keyboards Unsuitable for Unix Users
2002-12-18 09:41:18 anonymous2 [View]
-
Using Terminal in Jag to help update router firmware
2002-12-16 12:25:50 anonymous2 [View]
-
Using Terminal in Jag to help update router firmware
2002-12-21 05:48:08 maniabug [View]
-
crontab under /etc and /private/etc
2002-12-16 12:24:55 anonymous2 [View]
-
crontab under /etc and /private/etc
2002-12-20 10:36:21 Chris Stone |
[View]
-
print from pine using mac os X terminal
2002-12-14 16:04:15 fishclamor [View]
-
print from pine using mac os X terminal
2003-10-02 09:52:15 anonymous2 [View]
-
print from pine using mac os X terminal
2003-07-16 13:00:34 anonymous2 [View]
-
password??
2002-12-14 07:22:25 anonymous2 [View]
-
Answer From Chris_Stone
2003-01-18 16:41:16 pixm [View]
-
password??
2003-01-17 13:00:22 pixm [View]
-
Are you using root's password ?
2002-12-14 17:57:56 mike_dowe [View]
-
part 2?
2002-12-11 18:19:50 anonymous2 [View]
-
part 2?
2002-12-12 10:39:21 Chris Stone |
[View]
-
What about "crontab -e"?
2002-12-10 12:01:42 anonymous2 [View]
-
What about "crontab -e"?
2002-12-10 16:44:52 Chris Stone |
[View]
-
What about "crontab -e"?
2002-12-14 23:46:21 dillera [View]
-
What about "crontab -e"?
2002-12-20 10:32:21 Chris Stone |
[View]
-
log files just getting longer and bigger in 10.2.
2002-12-07 13:55:58 anonymous2 [View]
-
log files just getting longer and bigger in 10.2.
2002-12-09 07:52:46 charlesa1 [View]
-
What about startup items?
2002-12-07 05:10:38 astromac [View]
-
What about startup items?
2002-12-09 07:32:39 charlesa1 [View]
-
Request intermediate-level info
2002-12-06 19:53:14 myacavone [View]
-
Request intermediate-level info
2002-12-09 12:58:40 rainwadj [View]
-
and a book
2002-12-08 12:15:05 Daniel H. Steinberg |
[View]

