How to run a command before download with apt-get?
What about using apt pre/post-invoke hooks?
Essentially it is similar to dpkg pre/post-invoke hooks but for apt (which I'm assuming is actually the important one in your usage scenario). AFAIK there are others but I've only used like this.
E.g.
$ sudo cat /etc/apt/apt.conf.d/05new-hook
APT::Update::Pre-Invoke {"your-command-here"};
You can use an "alias" in the .bashrc file. For example, put that in your root .bashrc file:
alias apt-get='echo blahblah && apt-get'
Command-line arguments will be added automatically to the end of the alias.
Just replace echo blahblah
by the command you need.
But in this case, it only works when you're executing commands as root (not sudo).
First determine where is apt-get using whereis apt-get
or which apt-get
; I will assume it's in /usr/bin/apt-get
Then create a file in /usr/local/bin/apt-get with this content:
#!/bin/bash
# (commands to run before apt-get)
/usr/bin/apt-get "$@"
now chmod +x /usr/local/bin/apt-get
.
This should survive all upgrades of the distribution but it should not change the behavior of apt-get (aside from running the command before doing anything else). As bonus, it could parse the command line arguments to only run the command when it is truly needed.
(This answer builds upon the answer from Mohammad Etemaddar, merged with the feedback from muru)