macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

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 use sudo with care.
  • Youíll only need to enter your password when you havenít already used sudo within 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:

Pico

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:

  1. Choose a time when your Mac is likely to be on (and not asleep).
  2. 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.
  3. 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.

Quitting

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.

Return to the Mac DevCenter.