8

I am running a 12.04 LTS ubuntu server. And I think it would be nice if I could be informed when updates are available. But I can't find how to know...

I have tried looking at apt-get man page. From it I was able to use apt-get -s upgrade to get the apt-get output in a script without blocking on questions.

Now, I clearly see the différence :

Updates are available :

apt-get -s upgrade
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be upgraded:
  dpkg dpkg-dev libdpkg-perl
3 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Inst dpkg [1.16.1.2ubuntu7.2] (1.16.1.2ubuntu7.3 Ubuntu:12.04/precise-updates [amd64])
Conf dpkg (1.16.1.2ubuntu7.3 Ubuntu:12.04/precise-updates [amd64])
Inst dpkg-dev [1.16.1.2ubuntu7.2] (1.16.1.2ubuntu7.3 Ubuntu:12.04/precise-updates [all]) []
Inst libdpkg-perl [1.16.1.2ubuntu7.2] (1.16.1.2ubuntu7.3 Ubuntu:12.04/precise-updates [all])
Conf libdpkg-perl (1.16.1.2ubuntu7.3 Ubuntu:12.04/precise-updates [all])
Conf dpkg-dev (1.16.1.2ubuntu7.3 Ubuntu:12.04/precise-updates [all])

Updates are not available :

apt-get -s upgrade
Reading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

But I don't know how to proceed from there. How can I tell, from a bash script (or php script) that there are updates available or not ?

Edit :

Here is my current bash code. It does not work.

updates_available=`/etc/update-motd.d/90-updates-available`

if [ "${updates_available}" = "0 packages can be updated. 0 updates are security updates." ];
then
   echo "No updates are available"
else
   echo "There are updates available"
fi
  • Well it's only going to say 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. if there are not updates. – Nattgew Apr 29 '14 at 19:37
18

Read the man pages for motd(5), pam_motd(8) and update-motd(5). On my system, /etc/update-motd.d/90-updates-available invokes /usr/lib/update-notifier/update-motd-updates-available which displays this when I log in:

19 packages can be updated.
12 updates are security updates.

Delving a little deeper, the "...-updates-available" script invokes /usr/lib/update-notifier/apt-check --human-readable. If you read that (python), you'll see that if you omit the human readable flag, it will output "19;12" to stderr. We can grab that with this:

IFS=';' read updates security_updates < <(/usr/lib/update-notifier/apt-check 2>&1)
echo $updates
echo $security_updates 
19
12

Now you can say:

if (( updates == 0 )); then
    echo "No updates are available"
else
    echo "There are updates available"
fi
| improve this answer | |
  • I can go with that and test if [ /etc/update-motd.d/90-updates-available` = "0 packages can be updated. 0 updates are security updates." ]; then ... fi` – Adeline Apr 29 '14 at 20:08
  • Unfortunately,the check doesn't work (I added the forgotten quotes in my previous reply.) I suspect something to do with the new lines. I have updated my post with the code I derived from your reply. – Adeline Apr 29 '14 at 20:18
  • refined answer. – glenn jackman Apr 29 '14 at 20:32
  • Awesome answer, @glennjackman . I was going to answer with google.com/search?q=apt-get+check+if+updates+are+available #ImNotHelpfulToday =) – 0xSheepdog Apr 29 '14 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.