Creating a Pacman Hook to Run a Script After Package Upgrades in Arch Linux
Step‑by‑step guide to create a Pacman hook that runs a custom script after every package upgrade or install in Arch Linux.
27 Sept 2026, 10:28 UTC

Desired outcome
Configure a Pacman hook that automatically executes a custom script after every package upgrade or install transaction, without needing to reload any daemon.
Prerequisites
- Root access (or ability to use
sudo) to write to/etc/pacman.d/hooks/. - Pacman version 5.0 or newer (the hook mechanism has been stable since that release).
- The script you want to run must already exist and be executable (e.g.,
/usr/local/bin/post‑upgrade.sh). - Optional: enable Pacman logging by ensuring
LogFileis set in/etc/pacman.confif you want hook execution recorded in/var/log/pacman.log.
Procedure
- Create the hook file
As root, create a new file with a
.hookextension in the hooks directory:sudo touch /etc/pacman.d/hooks/post-upgrade.hook sudo chmod 644 /etc/pacman.d/hooks/post-upgrade.hook sudo nano /etc/pacman.d/hooks/post-upgrade.hookInsert the following content, adjusting the
Execpath to point to your script:[Trigger] Operation=Upgrade Operation=Install Type=Package Target=* [Action] When=PostTransaction Exec=/usr/local/bin/post-upgrade.shSave and exit the editor.
- Verify the hook is readable
Pacman will ignore a hook if the file is not world‑readable or if the syntax is wrong. Ensure the file permissions are
644(readable by all).ls -l /etc/pacman.d/hooks/post-upgrade.hook # Expected: -rw-r--r-- 1 root root ... - Test the hook with a safe transaction
Run a non‑disruptive Pacman operation, for example reinstalling the Pacman package itself:
sudo pacman -Syu --needed pacmanAfter the command finishes, check that your script executed. If your script writes to a log file, inspect that log; otherwise, add a temporary logging line (e.g.,
logger 'Hook ran') to verify. - Check Pacman’s internal log (optional)
If
LogFileis enabled in/etc/pacman.conf, look for hook execution entries:grep "running hook" /var/log/pacman.log # You should see a line referencing post-upgrade.hook
Expected checks
- Pacman exits with status 0 (no abort).
- The package database remains consistent:
sudo pacman -Qkreports no missing files. - Your script’s intended side‑effect (e.g., updating a database, sending a notification) occurs after the transaction completes.
Recovery / rollback options
Creating the hook file modifies system state. To undo the change:
- Remove or rename the hook file:
- Run another Pacman transaction to confirm the hook no longer triggers (your script should not run).
sudo mv /etc/pacman.d/hooks/post-upgrade.hook /etc/pacman.d/hooks/post-upgrade.hook.disabled
# or
sudo rm /etc/pacman.d/hooks/post-upgrade.hook
Limitations and cautions
- Hooks run with the same privileges as Pacman (typically root). Only use scripts you trust and have audited.
- If the hook script returns a non‑zero exit status, Pacman treats it as an error and aborts the transaction. Ensure your script returns
0on success. - Avoid actions inside the hook that modify the Pacman database or interfere with the ongoing transaction (e.g., removing packages).
- Some graphical front‑ends may bypass Pacman directly; test with plain
pacmancommands to guarantee hook execution. - If Pacman’s logging is disabled (
UseSyslogoff and noLogFile), you will not see automatic hook logs; rely on your script’s own output or manual checks.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.