3

Running Ubuntu 14.04 in a public computer room, I have enabled unattended-upgrades and configured to reboot at 02.00 in the night. PCs are dual boot, so a user would reboot the computer in order to boot the other OS, but if u-u had installed some updates, and set it to reboot, then the users would not be able to do it by themselves: if they click on the gear icon, and then choose shutdown and Reboot, the system simply does nothing.

If I login as root and try to issue a reboot command, the answer is "another shutdown already pending", so I have to shutdown -c to cancel it, and thereafter I am able to reboot. This is obviously not what I want, because the users should be able to reboot whenever they want without my (or root) intervention.

Could anyone help me please in reaching my goal (=have u-u set to install automatically, and reboot at 2.00 in the night, letting the users free to reboot/shutdown when they want, without giving them root permissions)?

Thank you very much,

Andrea

1
  • Sounds like you have conflicting requirements. Feb 23, 2018 at 11:25

1 Answer 1

3

Stop Unattended-Upgrades from restarting the computer

Right now Unattended-Upgrades is setup to restart the computer at 2:00AM if the update requires a restart. This step will stop this behavior.

Step: edit the file /etc/apt/apt.conf.d/50unattended-upgrades

Inside the file look for the line:

unattended-Upgrade::Automatic-Reboot "true";

and make it look like:

Unattended-Upgrade::Automatic-Reboot "false";

Create a bash script to check if restart is required and do it if needed

The script below checks if the file reboot-required exists and if so, it reboots the computer immediately. Let's call this file reboot_if_needed.sh.

#!/bin/bash
if [ -f /var/run/reboot-required ]; then
        echo $(date) Sytem restart required by: $(cat /var/run/reboot-required.pkgs)
        /sbin/reboot now
fi

Save this file as /opt/bin/reboot_if_needed.sh

Make this file executable:

sudo chmod +x /opt/bin/reboot_if_needed.sh

Explanation

When the script runs and the file var/run/reboot-required exists it will output some text that will be captured in a log file /var/log/reboot_history.log. Then the system will restart.

Schedule reboot_if_needed.sh every night at 2:00 AM

We want to run the script with administrator privileges. So we use sudo. Open a terminal by pressing Ctrl+Alt+T and enter:

sudo crontab -e

This command will open the crontab file for the root user if one exists, or create a blank new file. Add the following line at the end of the file:

0 02 * * * /opt/bin/reboot_if_needed.sh >> /var/log/reboot_history.log

If you use nano as the text file editor, exit the editor by pressing Ctrl+X. The editor will prompt you to save the changes. Press Y and then press Enter to select the default file name.

Explanation

Each line in crontab has five time-and-date fields, followed by a command, followed by a newline character ('\n'). The fields are separated by spaces. The five time-and-date fields cannot contain spaces. The five time-and-date fields are as follows:

  1. minute (0-59),
  2. hour (0-23, 0 = midnight),
  3. day (1-31),
  4. month (1-12),
  5. weekday (0-6, 0 = Sunday).

Therefore, 0 02 * * * means at 2:00 AM every night.

The line added to the crontab will make the script reboot_if_needed.sh run every night at 2 AM. The >> redirects the output to the /var/log/reboot_history.log file. This file will be created the first time crontab runs the script.

See https://help.ubuntu.com/community/CronHowto for how to use crontab

Why this works?

Unattended-Upgrade apparently schedules the restart with a delay. For example:

sudo shutdown -r 02:00

The above command schedules the restart at 2 AM. When such a command is in effect you get the message "another shutdown already pending".

We use crontab to take care of the starting the restart process at 2 AM, instead of scheduling it from within the shutdown command. Thus, there is no shutdown pending once Unattended-Upgrade has run. Any user can reboot the computer.

When any user reboots the computer the file /var/run/reboot-required if it exits is deleted by the system as reboot is not required any more. The computer will not be restarted at 2 AM by the script.

Hope this helps

8
  • Thank you, your first comment just lead me to the idea of a cron script, and furthermore your detailed explanation is excellent. I will try it and report here the results. Thank you!
    – Angus73
    Feb 23, 2018 at 17:40
  • You are welcome. Note that I have not tried it out. So, there may be some errors in the details. If you have a problem let me know in the comments. I will fix my answer so that it helps others.
    – user68186
    Feb 23, 2018 at 17:50
  • I can confirm that it works perfectly as expected. Thanks again!
    – Angus73
    Mar 9, 2018 at 13:20
  • I added logging of each automated restart. The log file will include a list of updated packages that needed restart.
    – user68186
    Mar 15, 2018 at 14:09
  • Thank you, that's great :-) Please could you just explain me why /opt/bin/reboot_if_needed.sh and not /opt/bin/reboot_if_needed.sh ? I mean, I know what the backticks mean but I don't catch why we use them here. Or, at least, I don't catch the difference between using them and not using them. Thank you!
    – Angus73
    Nov 15, 2018 at 7:42

You must log in to answer this question.

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