Advanced spec file items

A spec file can contain advanced items, such as Scriptlets, File triggers, and Triggers. These directives influence not only the spec file, but also the target operating system on which the resulting RPM is installed by updating the system with information from the RPM.

Scriptlets, File Triggers, and Triggers take effect at different points during the installation process on the target system, not the build process:

  • Scriptlets are unconditionally executed before or after the package is installed or deleted.
  • Triggers are conditionally executed when their specified trigger condition matches other packages on the installed system or in a transaction.
  • File triggers are conditionally executed when their specified path prefix matches other files on the installed system or in a transaction.

Scriptlets directives

Scriptlets are arbitrary programs that execute at pre-determined slots related to a package’s lifetime, for example, before or after the package is installed or deleted. Use scriptlets only for tasks that cannot be done at build time or in a startup script.

By default, scriptlets are short shell scripts declared by the various %pre or %post directives in a spec file.

Common scriptlet directives are similar to the spec file section headers, such as %build or %install. They are defined by multi-line segments of code, which are often written as a standard POSIX shell script. However, they can also be written in other programming languages that RPM accepts for the target machine’s distribution.

Scriptlets directives execution order

Review the order in which scriptlets are executed during the package upgrade.

Table 1. Scriptlets directives
Directive Description

%pretrans

The %pretrans scriptlet is executed before installing or removing a package.

%pre

The %pre scriptlet is executed before installing a package on the target system.

%post

The %post scriptlet is executed after a package was installed on the target system.

%preun

The %preun scriptlet is executed before uninstalling a package from the target system.

%postun

The %postun scriptlet is executed after a package was uninstalled from the target system.

%posttrans

The %posttrans scriptlet is executed at the end of a transaction.

Turn off scriptlets execution

Turn off the execution of any scriptlet by using the --no option with the scriptlet name.

About this task

Use the --noscripts option to turn off the execution of all the following scriptlets:

  • --nopre
  • --nopost
  • --nopreun
  • --nopostun
  • --nopretrans
  • --noposttrans
  • --nopreuntrans
  • --nopostuntrans

For more information, see the rpm man page on your system.

Procedure

Turn off the scriptlet execution:
# rpm --no<scriptlet_name>

For example, to turn off the execution of the %pretrans scriptlet, enter:

# rpm --nopretrans

Example macros for scriptlets

The following are example macros that you can use for scriptlets in a spec file. These macros are real-world macros provided by the systemd-rpm-macros package. You can use these macros for packaging systemd-related things. You can also apply similar principles to other packages.

Packages that contain systemd unit files must use scriptlets to properly handle systemd services. systemd packages provide a set of macros to handle systemd scriptlet operations. For example:

%post
%systemd_post httpd.service

%preun
%systemd_preun httpd.service

These macros expand to the following in a package:

$ rpm --eval "%systemd_preun httpd.service"
if [ $1 -eq 0 ] && [ -x "/usr/lib/systemd/systemd-update-helper" ]; then
    # Package removal, not upgrade
    /usr/lib/systemd/systemd-update-helper remove-system-units httpd.service || :
fi
Note
The way macros behave is subject to change. For example, macros' behavior can change with a package’s development.