This case study modifies crontab files during package installation.
This case study demonstrates the following techniques:
Using classes and class action scripts
For more information, see Writing Class Action Scripts.
Using the crontab command within a class action script.
The most efficient way to edit more than one file during installation is to define a class and provide a class action script. If you used the build class approach, you would need to deliver one build class script for each crontab file edited. Defining a cron class provides a more general approach. To edit crontab files with this approach, you must:
Define the crontab files that are to be edited in the prototype file.
Create an entry in the prototype file for each crontab file that will be edited. Define the class as cron and the file type as e for each file. Use the actual name of the file to be edited.
Create the crontab files for the package.
These files contain the information you want added to the existing crontab files of the same name.
Create an installation class action script for the cron class.
The sample i.cron script performs the following procedures:
Determines the user ID (UID).
The i.cron script sets the variable user to the base name of the cron class script being processed. That name is the UID. For example, the base name of /var/spool/cron/crontabs/root is root, which is also the UID.
Executes crontab using the UID and the -l option.
Using the -l option tells crontab to send the contents of the crontab file for the defined user to the standard output.
Pipes the output of the crontab command to a sed script that removes any previous entries added with this installation technique.
Puts the edited output into a temporary file.
Adds the data file for the root UID (that was delivered with the package) to the temporary file and adds a tag so you will know where these entries came from.
Executes crontab with the same UID and gives it the temporary file as input.
Create a removal class action script for the cron class.
The r.cron script is the same as the installation script except there is no procedure to add information to the crontab file.
These procedures are performed for every file in the cron class.
The i.cron and r.cron scripts described below are executed by superuser. Editing another user's crontab file as superuser may have unpredictable results. If necessary, change the following entry in each script:
crontab $user < /tmp/$$crontab ||
su $user -c "crontab /tmp/$$crontab" ||
PKG=case7 NAME=Case Study #7 CATEGORY=application BASEDIR=/opt ARCH=SPARC VERSION=Version 1.0 CLASSES=cron
i pkginfo i i.cron i r.cron e cron /var/spool/cron/crontabs/root ? ? ? e cron /var/spool/cron/crontabs/sys ? ? ?
# PKGINST parameter provided by installation service while read src dest do user=`basename $dest` || exit 2 (crontab -l $user | sed -e "/#$PKGINST$/d" > /tmp/$$crontab) || exit 2 sed -e "s/$/#$PKGINST/" $src >> /tmp/$$crontab || exit 2 crontab $user < /tmp/$$crontab || exit 2 rm -f /tmp/$$crontab done exit 0
# PKGINST parameter provided by installation service while read path do user=`basename $path` || exit 2 (crontab -l $user | sed -e "/#$PKGINST$/d" > /tmp/$$crontab) || exit 2 crontab $user < /tmp/$$crontab || exit 2 rm -f /tmp/$$crontab done exit
41,1,21 * * * * /usr/lib/uucp/uudemon.hour > /dev/null 45 23 * * * ulimit 5000; /usr/bin/su uucp -c "/usr/lib/uucp/uudemon.cleanup" > /dev/null 2>&1 11,31,51 * * * * /usr/lib/uucp/uudemon.poll > /dev/null
0 * * * 0-6 /usr/lib/sa/sa1 20,40 8-17 * * 1-5 /usr/lib/sa/sa1 5 18 * * 1-5 /usr/lib/sa/sa2 -s 8:00 -e 18:01 -i 1200 -A
Note - If editing of a group of files will increase total file size by more than 10K, supply a space file so the pkgadd command can allow for this increase. For more information on the space file, see Reserving Additional Space on a Target System.