5

On an Ubuntu 16.04.3 LTS server I run some process monitors, but I also run apt manually as well as unattended-upgrades for security updates.

This sometimes causes the monitors to report and act on programs that were stopped by apt during apt upgrade.

Is there a way to run a command when unattended-upgrades/apt starts the upgrades (to stop the monitors) and again when unattended-upgrades/apt finishes (to restart the monitors)?

I found APT::Update::Pre-Invoke and APT::Update::Post-Invoke hooks but the apt update step is not the problem, and apt itself does not seem to have any pre/post upgrade hooks.

1 Answer 1

6

There are three hooks you can use for this:

  • DPkg::Pre-Invoke is run once, before all the package manipulation sequences in one apt invocation;
  • DPkg::Pre-Install-Pkgs is also run once, before the package manipulation sequences, after the Pre-Invoke hook, and with a list of all the packages which will be installed;
  • DPkg::Post-Invoke is run once, after the package manipulation sequences.

The documentation isn’t all that clear (do read man apt.conf though), but reading the source code supports the above. Specifying hooks also confirms this (for example, this is how apt-listbugs works).

3
  • 1
    Works just like I wanted: DPkg::Pre-Invoke is run once before the start of all package manipulation sequences for that 'apt upgrade' run, and DPkg::Post-Invoke is run once when they are all completed. Just wanted to clarify that they don't get run before each (any) package. Commented Nov 1, 2017 at 23:52
  • Indeed, that’s what I was trying to explain above, but my answer wasn’t clear enough (and that’s what’s not clear in the documentation either). Commented Nov 2, 2017 at 8:22
  • 1
    I found that the Pre-Invoke is being called for each package installation. It was called around 240 times for 40 packages Commented Nov 12, 2024 at 17:16

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.