How to trigger a script each time a package is installed/uninstalled

I want to maintain a list of user-installed packages. I have a script that generates this list. How can I have it run whenever a package is installed or uninstalled?

1 Like

What OS? CentOS based on Debian based?

Fedora.

i.e. after dnf install or dnf remove

yum-plugin-post-transaction-actions is what was used for yum, not sure if there’s something similar for dnf, this package might still work as expected for dnf.

I don’t think so given this-

Just check
yum history

Ok, that’s too bad.

Nail in the coffin.

Also the description for yum-plugin-post-transaction-actions says

This plugin allows the user to run arbitrary actions immediately following a transaction when specified packages are changed.

That’s not exactly what I want. I’d like the script to run after any package is user-installed or user-uninstalled, and ideally avoid unnecessarily triggering it on upgrades.

The only ways that I can think of doing this is having a daemon tail the system logs and trigger your script when the condition is satisfied, or just have your script run as a cron job based on your desired frequency.

In the debian world, you have the ability to do this. I think in the SuSe world, this can be done with zypper. You may want to see if you can install Zypper on your system?

zypper is available in Fedora’s repo, but I have no familiarity with it.

I guess I’ll just let it be and have my script run before system backups.

If dnf had a general post-transaction scripting option that would be nice. A daemon watching system logs or a cron job are both probably doing too much work relative to how often packages are installed/removed.

Fair enough. If you don’t mind me asking, what is your end goal of knowing which packages are user installed?

You could just watch for the process and then do something when it ends.

The goal to keep track what (end-user, not system or dependency) packages I’ve installed myself on top of the base OS so that I can reinstall them in case I need to do a clean install of the OS. Ideally it would never need to be used.

It could also be useful for migrating to a new system.

I suppose but it’s not every dnf process that’s relevant – only the ones where I’m installing/removing a package.

you could alias yum for the users so that it ran a command/script of yours before/while/after it was doing the yum install

If you know programming and inotify… you could likely watch the yum/dnf database and trigger something off of that.

I’m not familiar with yum/dnf or inotify. I guess my laziness wins over when it gets this deep in the weeds. Or I could call it “optimizing my time” :smiley:

Could you not write a script that checks yum history and last… to send an alert when something changes?

I’m not sure that would be enough because I don’t want to keep track of system packages or automatically installed dependencies. I guess that could be used as the trigger to run my script. I feel that would overly complicate the matter.

The script I’m currently using is simple and fast (< 1s) and doesn’t need root/sudo:

LIST_FILE=~/Dropbox/userinstalled_$(hostname).txt

dnf --cacheonly repoquery --qf "%{name}" --userinstalled > $LIST_FILE

The only question is if there’s a simple way to trigger it when a package is installed or removed by the user. Otherwise, I’ll just have it run automatically before system backups are made and that’s likely good enough.

yum history info 1

would give you more details on what was installed/updated

Yes, but I don’t see how those details are useful. Rather, they’re problems:

  • the list can’t be passed to dnf to install packages due to formatting and other info
  • instead of generic package names, they list specific package names that unhelpfully also contain the version, architecture, and fedora version in the filename
  • to my knowledge it’s not clear which were installed by the user or by a system update or as dependencies
  • more minor issue, but accessing transaction history requires root/sudo

Also, I’m not sure if it’s easy even to just keep track of new transaction events and trigger my script. I think transaction history is stored in a database, not a file?