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.
The Maintenance ScriptsA 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:
The periodic CommandMaintenance 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 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
Add Your Own MaintenanceIt'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
Launching Maintenance ScriptsThe 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
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
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 TasksIf 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
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 |