Project72.Manage Periodic Maintenance


Project 72. Manage Periodic Maintenance

"Does Unix dust and polish?"

This project covers periodic maintenance. It shows you how Unix manages routine housekeeping tasks such as deleting temporary files and rebuilding databases; it also shows you how these tasks can be extended to include locally defined maintenance. Project 70 covers one-off scheduling with the at command. Project 71 covers periodic scheduling with the system cron daemon.

Rotating Log Files

To ensure that log files do not grow indefinitely, they are rotated. Rotation involves compressing the file and archiving it under a name such as system.log.gz.0. A formerly archived log file of that name is first renamed system.log.gz.1; the old 1 file in turn is renamed 2, 2 is renamed 3, and so on, thus keeping an archive of older log files. The oldest file is overwritten without being renamed. Type

$ ls /var/log


and you'll see the archived log files.


The Maintenance Scripts

A file system will become cluttered and dated if it is not cleaned periodically; Mac OS X has several shell scripts for this purpose. Specifically, it has daily, weekly, and monthly scripts that run (dare I say it?) daily, weekly, and monthly. The scripts are launched automatically, in the background and without user intervention. Even though this happens automatically, it's good to know how maintenance works and how to change or extend it when necessary.

The standard Mac OS X maintenance scripts perform the following tasks:

  • Daily removes old crash-log files, temporary and junk files; backs up the local NetInfo database; rotates the system log files; and runs the local daily-maintenance script.

  • Weekly updates the locate database (Project 15) and the whatis database (Project 3); rotates the ftp, lookupd, lpr, mail, netinfo, hwmond, ipfw, ppp, and secure log files in the directory /var/log; and runs the local weekly-maintenance script.

  • Monthly rotates the wtmp, install, and fax log files, and runs the local monthly-maintenance script.

The periodic Command

Maintenance scripts are run as the root user from the periodic command. Apple's Launch Daemon issues the necessary commands at specific intervals as

periodic daily periodic weekly periodic monthly


Tip

The periodic command can be configured. It executes a script from the file /etc/defaults/periodic.conf to set up the environment before it executes each maintenance script. Find out more by typing

$ man 5 periodic.conf



The periodic command looks in the appropriate directory within /etc/periodic and executes all scripts it finds there. When given the argument daily, for example, it looks for scripts in /etc/periodic/daily/. List all such script files by typing

$ ls -R /etc/periodic


When a periodic script runs, it writes an audit trail to a file in /var/log: daily.out, weekly.out, or monthly.out. Examine these by using the less command or any text editor.

Note

Never change the Apple-supplied scripts when adding your own maintenance commands: Use the local versions in /etc/*.local. It's possible that the scripts supplied by Apple will be overwritten by a future software update.


Add Your Own Maintenance

It's very easy to add your own maintenance tasks to the daily, weekly, and monthly scripts. Each script runs a corresponding local script called /etc/daily.local, /etc/weekly.local, or /etc/monthly.local. Commands added to those local files become part of the regular maintenance tasks. If the local script file does not already exist, create it.

Learn More

Projects 9 and 10 introduce shell scripting, and the projects in Chapter 9 cover the subject in more detail.


Launching Maintenance Scripts

The way in which maintenance scripts are launched depends on whether you're running Mac OS X 10.4 (Tiger) or any system before Tiger. We'll look at Tiger first.

The periodic command is launched by Apple's Launch Daemon. You might like to check the Launch Daemon configuration settings that run each of the three scripts. They are located in the following files in the directory /System/Library/LaunchDaemons/.

com.apple.periodic-daily.plist com.apple.periodic-weekly.plist com.apple.periodic-monthly.plist


Learn More

Project 40 talks about process priority and the nice command.


Use a text editor, or if you have installed the Developer Tools, double-click a file to open it in the Property List Editor (PLE) application. In either case, you'll see a property called StartCalendarInterval with subproperties Hour, Minute, Day, and Weekday. These subproperties state the time and date on which the script will be run. A property that is not specified is read as every. A missing Day property, for example, is taken to mean every day.

Learn More

The cron daemon and crontabs are discussed in Project 71.


Examining these files, you'll see that the daily script is run at 3:15 in the morning every day, because no day is specified. The weekly script is run at 3:15 on a Saturday (day 6), and the monthly script is run at 5:30 on the first of every month. If your Mac is not switched on when a script is scheduled to run, the script will be run when your Mac is next started.

You'll also notice that other options are provided, such as running the task at a low priority by specifying a nice value.

In versions of OS X before Tiger, periodic was scheduled by the more traditional Unix method of adding lines to the system crontab. You'll see the following lines in the file /etc/crontab.

# Run daily/weekly/monthly jobs. 15    3     *     *     *     root   periodic daily 30    4     *     *     6     root   periodic weekly 30    5     1     *     *     root   periodic monthly


The net effect is the same as for the Launch Daemon method employed by Tiger, with one exception: If your Mac is switched off at the scheduled times, the scripts will not be run later, and maintenance will miss a beat. You'll have to run the scripts by hand, typing a command such as

$ sudo periodic daily Password: ...


Add Your Own Scheduled Tasks

If you want to schedule recurring tasks at times other than those used for standard daily, weekly, and monthly maintenance, you can do so easily by configuring the Launch Daemon (introduced in OS X 10.4) or the system crontab (in any version of Mac OS X).

I scheduled custom backups of my server by adding the following lines to the system crontab in the file /etc/crontab.

# backup the server 00 5 * * * root /usr/local/sbin/bu-server ServerMadeData 30 5 * * 6 root /usr/local/sbin/bu-server UserAccounts all 00 6 1 * * root /usr/local/sbin/bu-server Partitions all 30 6 1 * * root /usr/local/sbin/bu-server SystemPartition


Alternatively, you may add a configuration file to Launch Daemon. Here's an example in which we add a new configuration file by copying and editing an existing one. We must always add local customizations to the directory /Librarynever to /System/Library. Let's create a configuration to run a simple test command such as

/usr/bin/touch /my-daily-test


We choose to use the file com.apple.periodic-daily.plist as a starting point for our new file.

$ cd /Library/LaunchDaemons/ $ sudo cp /System/Library/LaunchDaemons/¬     com.apple.periodic-daily.plist my-daily.plist


Next, we change the owner and permissions so that we (or any other administrator) can edit the file.

$ sudo chown root:admin my-daily.plist $ sudo chmod g+w my-daily.plist $ ls -l -rw-rw-r-- 1 root admin 579 Aug 16 12:46 my-daily.plist


Edit the file by using the OS X Property List Editor (in Developer Tools, if you've installed them) or any text editor. Here's an example, shown in Figure 8.1, in which we schedule our simple test command to run daily. Choose a time in the near future for testing purposes.

Figure 8.1. Apple's Property List Editor lets you view and edit XML-based plist files.


Tip

The format of the StartCalendarInterval property follows the semantics of a crontab entry, and a missing subproperty is equivalent to a star in the crontab column. Project 71 covers the cron daemon and crontabs.


Finally, use the launchctl command to load the configuration for our new task and start it.

$ sudo launchctl load /Library/LaunchDaemons/my-daily.plist $ sudo launchctl start my-daily $ sudo launchctl list ... my-daily


Wait until the scheduled time, and you'll see that the following file is created.

$ ls -l /my-daily-test -rw-r--r-- 1 root admin 0 Aug 16 13:24 /my-daily-text





Mac OS X UNIX 101 Byte-Sized Projects
Mac OS X Unix 101 Byte-Sized Projects
ISBN: 0321374118
EAN: 2147483647
Year: 2003
Pages: 153
Authors: Adrian Mayo

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net