3

With apt-get, I can use Pre- or Post-Invoke hooks

Pre-Invoke, Post-Invoke
This is a list of shell commands to run before/after invoking dpkg(1). Like options this must be specified in list notation. The commands are invoked in order using /bin/sh; should any fail APT will abort.

See also hook a script to apt-get

Is there a similar way to run a command before and after installing every package with RPM or DNF?

Wrapping the rpm or dnf command in another script would be one way of course, but I would rather have some configuration option.

2 Answers 2

3

For yum (RHEL/CentOS 7 and below) there are yum-plugin-pre-transaction-actions and yum-plugin-post-transaction-actions packages. Examples on how to use it for pre-transaction and post-transaction but here's a sample file too:

#action_key:transaction_state:command
# action_key can be: pkgglob, /path/to/file (wildcards allowed)
# transaction_state can be: install,update,remove,any
# command can be: any shell command
#  the following variables are allowed to be passed to any command:
#   $name - package name
#   $arch - package arch
#   $ver - package version
#   $rel - package release
#   $epoch - package epoch
#   $repoid - package repository id
#   $state - text string of state of the package in the transaction set
#
# file matches cannot be used with removes b/c we don't have the info available

*:install:touch /tmp/$name-installed
zsh:remove:touch /tmp/zsh-removed
zsh:install:touch /tmp/zsh-installed-also
/bin/z*h:install:touch /tmp/bin-zsh-installed
z*h:any:touch /tmp/bin-zsh-any

# each action is expanded once for each matching package, and no action is
# executed twice per transaction, for example
*:install:echo $repoid >>/tmp/repos
# will write each repo only once to /tmp/repos, even if multiple packages from
# the same repo were installed

For dnf (RHEL/CentOS 8 and up) there is a plugin at https://github.com/rpm-software-management/dnf-plugins-core/blob/master/plugins/post-transaction-actions.py for post-transaction, but nothing for pre-transaction. https://bugzilla.redhat.com/show_bug.cgi?id=967264 and https://bugzilla.redhat.com/show_bug.cgi?id=1788574 have more info . RHEL 8.2 is supposed to have the post-transaction feature. If you really need pre-transactions, you could modify the post-transaction code to create your own plugin for pre-transactions (and submit it as a PR).

0

You can define in an RPM a %pretrans scriptlet that runs before the package is installed. It isn’t exactly the same as Debian packaging, since it’s part of the package itself and not external.

1
  • Thank you, but I'm looking for a hook running for every package, basically the same as the "Pre-Invoke" and "Post-Invoke" hooks. I clarified my question. May 10, 2020 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.