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).