Product SiteDocumentation Site

4.2.7. Listing the scripts in a package

RPM packages can have preinstallation, postinstallation, preuninstallation, and postuninstallation scripts. These are scripts that the rpm command will execute before and after installing a package, as well as before and after removing the package. The --scripts option to the rpm –q command lists the scripts associated with a package. The basic syntax follows:
rpm -q --scripts package_name
For example:
rpm -q --scripts tcsh
if [ ! -f /etc/shells ]; then
 echo "/bin/tcsh" >> /etc/shells
 echo "/bin/csh"        >> /etc/shells
else
 grep -q '^/bin/tcsh$' /etc/shells || \
 echo "/bin/tcsh" >> /etc/shells
 grep -q '^/bin/csh$'  /etc/shells || \
 echo "/bin/csh"        >> /etc/shells
fi
postuninstall scriptlet (using /bin/sh):
if [ ! -x /bin/tcsh ]; then
 grep -v '^/bin/tcsh$'  /etc/shells | \
 grep -v '^/bin/csh$' > /etc/shells.rpm && \
 mv /etc/shells.rpm /etc/shells
fi
The simple scripts shown here add an entry to the /etc/shells file and clean up the entry when uninstalled.

Cleaning up

Cleaning up any changes your package makes on uninstallation is a very good idea. See the chapters in Part II, Creating RPMs, for details on making your own well-behaved packages.
Other packages have more complex scripts, as shown following:
rpm -q --scripts sendmail
preinstall scriptlet (using /bin/sh):
/usr/sbin/useradd -u 47 -d /var/spool/mqueue -r -s /sbin/nologin mailnull >/dev/null 2>&1
/usr/sbin/useradd -u 51 -d /var/spool/mqueue -r -s /sbin/nologin smmsp >/dev/null 2>&1
exit 0
postinstall scriptlet (using /bin/sh):
/sbin/chkconfig --add sendmail
# Set up the alternatives files for MTAs.
/usr/sbin/alternatives --install /usr/sbin/sendmail mta /usr/sbin/sendmail.sendmail 90 \
        --slave /usr/bin/mailq mta-mailq /usr/bin/mailq.sendmail \
        --slave /usr/bin/newaliases mta-newaliases /usr/bin/newaliases.sendmail \
        --slave /usr/bin/rmail mta-rmail /usr/bin/rmail.sendmail \
        --slave /usr/lib/sendmail mta-sendmail /usr/lib/sendmail.sendmail \
        --slave /etc/pam.d/smtp mta-pam /etc/pam.d/smtp.sendmail \
        --slave /usr/share/man/man8/sendmail.8.gz mta-sendmailman /usr/share/man/man8/sendmail.sendmail.8.gz \
        --slave /usr/share/man/man1/mailq.1.gz mta-mailqman /usr/share/man/man1/mailq.sendmail.1.gz \
        --slave /usr/share/man/man1/newaliases.1.gz mta-newaliasesman /usr/share/man/man1/newaliases.sendmail.1.gz \
        --slave /usr/share/man/man5/aliases.5.gz mta-aliasesman /usr/share/man/man5/aliases.sendmail.5.gz \
        --initscript sendmail

# Rebuild maps
{
        chown root /etc/aliases.db /etc/mail/access.db \
                /etc/mail/mailertable.db /etc/mail/domaintable.db \
                /etc/mail/virtusertable.db
        SM_FORCE_DBREBUILD=1 /etc/mail/make
        SM_FORCE_DBREBUILD=1 /etc/mail/make aliases
} > /dev/null 2>&1
exit 0
preuninstall scriptlet (using /bin/sh):
if [ $1 = 0 ]; then
        /etc/rc.d/init.d/sendmail stop >/dev/null 2>&1
        /sbin/chkconfig --del sendmail
        /usr/sbin/alternatives --remove mta /usr/sbin/sendmail.sendmail
fi
exit 0
postuninstall scriptlet (using /bin/sh):
if [ "$1" -ge "1" ]; then
        /etc/rc.d/init.d/sendmail condrestart >/dev/null 2>&1
        mta=`readlink /etc/alternatives/mta`
        if [ "$mta" == "/usr/sbin/sendmail.sendmail" ]; then
                /usr/sbin/alternatives --set mta /usr/sbin/sendmail.sendmail
        fi
fi
exit 0
In this case, the main script is the post-install script, which tries to convert old-format data into the new format, thereby helping users upgrade to the newer release.