5

I would like to start my script after or during every Ubuntu update. For example:

If I make command sudo apt-get upgrade, I want to start my script after successful update. Same if I make update through Software Updater. Is it possible?

1
  • After successful update or successful upgrade?
    – A.B.
    Commented Apr 26, 2015 at 15:40

2 Answers 2

2

How to start app or script after system upgrade

Watch for changes in /var/log/apt/history.log:

sudo inoticoming /var/log/apt/ upgrade_success {} \;

Script upgrade_success:

#!/bin/bash


if [ $1 == "history.log" ]
then

    echo "Changes in \"/var/log/apt/history.log\""

    if [ ! -f "/tmp/upgrade_success_last_run" ] || [ $(stat -c %Y "/tmp/upgrade_success_last_run") -lt $(date "+%s") ]
    then
        # look for changes since the last run
        # of this script ("upgrade_success")
        # in "/var/log/apt/history.log"
        # and make what you want.
    fi

    touch /tmp/upgrade_success_last_run

fi

How to start app or script after system update

You write the following:

...
I would like to start my script after or during every Ubuntu update.
...


Create this file:

/etc/apt/apt.conf.d/16my-script

and add this line:

APT::Update::Post-Invoke-Success {"<path_to_your_script> 2>/dev/null || true";};

Unfortunately, there is no way to do the same for Upgrade, eg.:

APT::Upgrade::Post-Invoke-Success {"<path_to_your_script> 2>/dev/null || true";};

OR


In the file /etc/apt/apt.conf.d/15update-stamp

you can find this line:

APT::Update::Post-Invoke-Success {"touch /var/lib/apt/periodic/update-success-stamp 2>/dev/null || true";};

Therefore you can watch for changes of the file /var/lib/apt/periodic/update-success-stamp. After any update command, even through Software Updater, this file's modification date is the date of the last successful update.

Watch the folder /var/lib/apt/periodic/ with inoticoming:

sudo inoticoming /var/lib/apt/periodic/ upgrade {} \;

Script upgrade:

#!/bin/bash

if [ $1 == "update-success-stamp" ]
then
    sudo apt-get upgrade
fi

And also here: Unfortunately, there is no way to do the same for Upgrade, eg.:

APT::Upgrade::Post-Invoke-Success {"touch /var/lib/apt/periodic/upgrade-success-stamp 2>/dev/null || true";};

and watch for changes of the file /var/lib/apt/periodic/upgrade-success-stamp.

1
  • @PeterMaciejko: As you're a reputation 1 user: If this answer helped you, don't forget to click the grey at the left of this text, which means "yes, this answer is valid"! ;-) A.B.: Good one! Upvoted!
    – Fabby
    Commented Apr 26, 2015 at 22:58
1

You can do this by adding a say upgrade alias to ~/.bashrc, and by running the alias to perform the upgrade:

  1. Mark your script as executable: chmod +x <path_to_script>

*<path_to_script> = path to the script you want to run

  1. Add an upgrade alias to ~/.bashrc: <<< "alias upgrade='sudo apt-get upgrade && <path_to_script>'" tee -a ~/.bashrc

*<path_to_script> = path to the script you want to run

  1. Update ~/.bashrc: . ~/.bashrc

This way every time you run upgrade both sudo apt-get upgrade and your script will be executed, the script only upon success of sudo apt-get upgrade.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .