oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

An Introduction to Tiger Terminal, Part 5
Pages: 1, 2, 3, 4, 5

You can edit the times if you want the tasks to run at more reasonable times. Use the arrow keys to move the cursor to the field you want to change, delete number and type in a new one. For example, if you wanted to change the weekly script from 4:30 a.m. to 10:00 p.m. and from day 6 (Saturday) to day 7 (Sunday), you would change this line:

30 4 * * 6 root periodic weekly

to this line:

0 22 * * 7 root periodic weekly

Each of the three command lines in the crontab file runs the periodic command along with the argument of daily, weekly, or monthly. The argument (daily, weekly, or monthly) to periodic is the name of a directory inside the /etc/periodic directory. For example, periodic daily means the periodic command looks inside the /etc/periodic/daily directory and alphabetically executes every file in that directory. There are two files in daily (100.clean-logs and 500.daily) and one (500.daily) in each of the weekly and monthly directories.

Where is cron in Tiger? Well, cron is still there on the system, although deprecated, most likely for backward compatibility issues due to the evolving nature of launchd. Open crontab using nano at the command line (sudo nano /etc/crontab) and you'll be prompted for your password:

figure 3
Figure 3. sudo nano /etc/crontab

Then you'll see this message:

figure 4
Figure 4. System crontab message

Or, since you're just reading the file, you could have used the cat command to look at the contents of the crontab file:

figure 5
Figure 5. cat /etc/crontab

In OS X 10.4, instead of starting at boot and checking in every minute, cron is only started when called. The system crontab still exists in the /etc directory and cron will continue to work if you add a new cron job to the directory /var/cron/tabs or if you change the crontab file in the /etc directory.

In order to utilize cron in Tiger and create your own scheduled job, you would use the crontab command with the -e option (for edit). This command uses the vi editor by default. Since we're more comfortable using nano, let's see about changing that because we'll be creating our own cron job. First, open and let's see what environment variables are defined by using the command, env.

figure 6
Figure 6. Looking at environment variables using the env command

We can set our default editor for crontab by adding a $VISUAL variable and naming nano.

figure 7
Figure 7. Adding $VISUAL variable

We've set our editor using the export VISUAL=nano command to send the variable into the environment. This means that the variable will be maintained if our shell creates any child processes (but not if we close the terminal!). Then, we tested it by using the echo command.

If you want to make this change permanent, you would edit the bash shell config file in your home directory (refer to Part 1 if you need a refresher).

figure 8
Figure 8. Edit your bash config file

Add the environment variable at the end of your .bash_profile configuration file.

figure 9
Figure 9. Add the environment variable

Save it (^X, confirm with Y then hit the Enter key to write to .bash_profile).

Close the window, open a new window and test your changes by typing echo $VISUAL.

figure 10
Figure 10. echo $VISUAL

Now we can create a simple cron job and have it run every week at a set day and time. Our job will repair permissions on our boot volume and then output the results to a file on our desktop. The diskutil command is the CLI version of the Disk Utility app, found in /Applications/Utilities/. Type crontab -e at the command line.

figure 11
Figure 11. crontab -e

Recall that we made nano our default editor above so you won't have to learn vi just for this exercise. We'll be telling cron to run our job every particular weekday at a particular time. Since this is an exercise and we don't want to wait forever for this to run, I'm going to set mine for 5 minutes from now. If you need a bit more time, plan accordingly. You'll tab between fields, with the fields running: minute (0-59), hour (0-23), day of the month (1-31), month (1-12), day of the week (0-7 with Monday=1, Sunday=7), and command to be executed. The "/" after the repairPermissions option indicates that the command is run on the startup volume.

Set this for a few minutes from now. Note that my username (norburym) is indicated. Use yours instead! Don't worry about the ugly file name!

Here is a screen shot of the command in nano:

figure 12
Figure 12. cron job

If you wanted to see the display as output, type crontab -l at the command line:

05 20 * * 1 /usr/sbin/diskutil repairPermissions / >> /Users/norburym/Desktop/fixed.txt

When cron runs this job, you'll see a text file appear on your desktop called fixed.txt:

figure 13
Figure 13. fixed.txt file generated by our cron job

To remove the cron job, go to the command line and type:

crontab -r

Check to make sure it's gone by typing crontab -l and you should see:

crontab: no crontab for yourusername

Pages: 1, 2, 3, 4, 5

Next Pagearrow