How to run a command or script after running apt-get command on a Debian or Ubuntu Linux

How do I run custom command after ‘apt-get upgrade‘ or ‘apt-get dist-upgrade‘ on a Debian or Ubuntu Linux based system? How do I hook a script to apt-get command on my Ubuntu Linux server?

You can configure the apt-get command using /etc/apt/apt.conf file or place configuration in a special directory at /etc/apt/apt.conf.d/. The following two options allows you to run shell commands or script before/after invoking dpkg/apt-get tools:


The syntax is:

# This is a list of shell commands to run after invoking dpkg/apt-get #
DPkg::Post-Invoke {"command";};
DPkg::Post-Invoke {"/path/to/sbin/command";};
DPkg::Post-Invoke {"/path/to/script";};


The syntax is:

# This is a list of shell commands to run before invoking dpkg/apt-get #
DPkg::Pre-Invoke {"command";};
DPkg::Pre-Invoke {"/path/to/sbin/command";};
DPkg::Pre-Invoke {"/path/to/script";};

Like options this must be specified in list notation. The commands are invoked in order using /bin/sh; should any fail APT will abort.


In this example auto re-mounting of a readonly /use/ folder so that apt-get will work properly. Edit or create a file called /etc/apt/apt.conf.d/100update:
$ sudo vi /etc/apt/apt.conf.d/100update
Append/edit as follows:

   Pre-Invoke {"/bin/mount -o remount,rw /usr/";};
   Post-Invoke {"/bin/mount -o remount,ro /usr/";};

Another example:

     Pre-Invoke {"touch /var/lib/apt/pre-update-stamp"; };
     Post-Invoke {"touch /var/lib/apt/post-update-stamp"; };

Running or hooking a custom script to apt-get on a Debian or Ubuntu Linux server

In this example I want to run the following script (/root/bin/php7helper) so that I can patch config file upon running apt-get command:

# Name: /root/bin/php7helper
# Purpose: A shell script to patch php 7 fpm on Ubuntu serer
# Author: Vivek Gite <> under GPL version 2+
# ------
function update_php7(){
	local u="cyberciti"  # new user name
	local r="www-data"   # old user name
	local f="/usr/lib/tmpfiles.d/php7.0-fpm.conf" # config file
	local wrcmd="/etc/init.d/lighttpd restart"    # restart services
	local prcmd="/etc/init.d/php7.0-fpm restart"
	# patch it
	echo "$0: Patching $f..."
	sed -i "s/$r/$u/g" $f  
	# restart it
# main #

Create a file called /etc/apt/apt.conf.d/80upgradehook:
$ cat /etc/apt/apt.conf.d/80upgradehook
Sample outputs:

DPkg::Post-Invoke {"/root/bin/php7helper";};

Now simply run the apt-get upgrade command and the script /root/bin/php7helper will get executed:
$ sudo apt-get upgrade
Sample outputs:

Fig.01: apt-get hook called a script called /root/bin/php7helper after running it

See man pages for more info – dpkg(1)

🐧 Please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
4 comments… add one
  • juriskaste Feb 17, 2016 @ 14:12

    Hi. I’ve never attempted to think of such scenario, yet .
    Interesting example though, thanks ! :)
    No offense, but I noticed a typo “You can configuration apt-get command” – shouldn’t it be “You can configure apt-get command” ?

  • Christian Rusa Mar 4, 2016 @ 7:03

    Thanks for this article gave me some ideas. :-)

    I guess the filename 100update in the example is supposed to put it as last file in the directory. As all the other files there only have two digits the file beginning with 100 ends up among the 10s in lexical order and not at the end.
    But not really an issue.

  • DvnBthlo May 22, 2017 @ 10:58

    Hello, how run the script after if update only xxx package ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @