Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.
share|improve this question

3 Answers 3

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.

share|improve this answer

One sub-par way---maybe specific to Ubuntu---is to check the existence of this file:

/var/run/reboot-required

If it exists, a reboot is required.

So a Bash function in .bash_rc (and co.) could do the job:

function needs_reboot {
  if [ -f /var/run/reboot-required ]; then
    read -p "Restart needed. Reboot? (y/N) " answer;
    if [ "$answer" = "y" -o "$answer" = "Y" ]; then
      reboot;
    fi
  else
    echo "No restart";
  fi
}

Where does this idea come from? MOTD gets updated at login time by the pam_motd process, reading its contents from the /etc/update-motd.d directory. In this directory, there is a /etc/update-motd.d/98-reboot-required script (on Ubuntu 12_04) that checks for the existence of our /var/run/reboot-required. That's apparently how a reboot advice is decided. So the function above just makes the same check, interactively.

I'd really like to get that directly from APT, but for now, this works...

share|improve this answer

You only need to reboot if the kernel is upgraded.

share|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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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