macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

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 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.

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:

Screen shot.

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:

  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 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.

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.

Screen shot.

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.