8

I need this for a simple monitoring script which i don't want to run as root...

2
  • 1
    there's always command-restricted nopasswd sudo Jul 7, 2011 at 16:31
  • Thanks to all of you for the many answers. After trying a few of your suggestions and digging around a little more it seems to me that the solution that requires the least dependencies and is the most secure is simply parsing /etc/motd - however that doesn't seem very elegant to me :) Jul 7, 2011 at 18:28

3 Answers 3

10

The number of pending security updates can be found using:

/usr/lib/update-notifier/apt-check 2>&1 | cut -d ';' -f 1

and the number of pending regular updates can be found using:

/usr/lib/update-notifier/apt-check 2>&1 | cut -d ';' -f 2

https://superuser.com/questions/199869/check-number-of-pending-security-updates-in-ubuntu

3
  • much more elegant than mine :) Jul 7, 2011 at 17:01
  • Thanks for the reply however as pointed out by jldugger it is a (headless) server so installing >240 packages including lot's of gui stuff just to get the number of apt updates really isn't an option. Jul 7, 2011 at 18:20
  • It's provided by the update-notifier-common package. I don't think it has any gui dependencies.
    – sourcejedi
    Feb 25, 2013 at 22:56
1

using sudo is your answer, you will likely need to add the user you want to run the script at to the sudoers file and probably with the nopasswd flag so it doesnt prompt for your password everytime the script runs, for more info about sudo, try taking a look here: https://help.ubuntu.com/community/Sudoers

2
  • I'd really rather not have anything run as root on a server that doesn't absolutely have to. Jul 7, 2011 at 18:18
  • it only runs as root long enough to run the command, and you can restrict it to the specific command needed to get updates, thus neutralising any possible security threat Jul 7, 2011 at 19:38
1

Ubuntu has many packaged nagios plugins, including the handy check_apt, a custom C program. It's slightly better than update-notifier in that its dependencies are smaller; if you're interested in monitoring servers you've probably already installed it! The output looks like this (if you don't like it it seems trivial to fork the program to print what you need):

APT OK: 0 packages available for upgrade (0 critical updates).

If nagios alerting isn't your preference, you can do what I do and install apticron, which will send you email notifications about available updates on a system.

3
  • Thanks! Apticron isn't an option as i need the information for my own script so an email doesn't do the trick. For the same reason i'm not using nagios, however you answer seems to imply that "check_apt" can be used independently ? Jul 7, 2011 at 18:23
  • @Joe all of the nagios plugins can be run independently-- that's most of what nagios does :) Jul 7, 2011 at 18:34
  • Yep. It's just a program installed to /usr/lib/nagios/check_apt. Runs just fine as any old user.
    – jldugger
    Jul 7, 2011 at 18:34

You must log in to answer this question.

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