I have a PC running Ubuntu server.

Sometimes when I ssh into it, I get a message that some packages need upgrading.

I upgrade the software by running (as root)

aptitude update && aptitude safe-upgrade

Sometimes that is enough. But sometimes I need to reboot as well, and I am not told this until I ssh in subsequently. How can I find out sooner?

Ideally I'd like to upgrade by running something like

aptitude update && aptitude safe-upgrade &&
if aptitude needs-reboot ; then shutdown -r now ; fi

but the aptitude needs-reboot command does not exist. What should I run instead?


Clarifications:

  • Actually I have two PCs, one running 10.04 (lucid) and the other running 11.04 (natty). I'd hope it would be the same solution for both.
  • Strictly command line solutions only please. One of these PCs doesn't have a display, and neither of them has a keyboard.
link|improve this question

75% accept rate
feedback

2 Answers

You only need to reboot if the kernel is upgraded.

link|improve this answer
Is there an easy way of scripting that, without resorting to parsing the output of aptitude safe-upgrade? – dave4420 Jul 29 '11 at 17:30
feedback

How about this (run as root): apt-get update && apt-get dist-upgrade --dry-run | grep linux-; if [ $? -eq 0 ]; then yes | apt-get dist-upgrade && reboot; else yes | apt-get dist-upgrade; fi

If there is a kernel update grep linux- will return 0 and then you will reboot. The yes command could be replaced by apt-get dist-upgrade --assume-yes. There will be no reboot if no kernel update is found.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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