22

I have installed a (x)Ubuntu 14.04 on a friend's PC. Automatic updates are set to "download and automatic install updates".

The problem is that, after some months of use, he unknowingly shuts down his PC before packages upgrade complete. That leads to broken dependencies/packages, which leads to updating being affected and the need to run sudo dpkg --configure -a

Is it possible to make Ubuntu wait for the updates to complete before PC shutdown or reboot like Windows does to ensure that there will never be broken packages and that his PC will remain updated automatically?

DK Bose
  • 42,548
  • 23
  • 127
  • 221
Giannis
  • 476
  • 4
  • 12
  • Have you let your PC afk while upating?. Check your System Settings -> Power – BeGood Nov 24 '15 at 23:41
  • 1
    What do you think is causing the shutdowns to occur? An OS should not just randomly shutdown on its own, as that would also be a data loss risk. Is he perhaps pressing the power button or cutting the mains? – thomasrutter Nov 24 '15 at 23:57
  • It does not shutdown randomly. It is user choice. But after many months of use he happens to shutdown the pc while updating is still on progress – Giannis Nov 25 '15 at 09:45
  • I'm in a similar but worse bind as I told my server to reboot durring a dist-upgrade that I'd manually started (and forgotten about). It's clear that it was in the middle of updating grub at the time so now I need to fix that (not impossible, but I'm glad this isn't a system I really need up and running right now). – LovesTha Dec 24 '15 at 22:49
  • I'm writing a bash script to try to do this, I'll post it when I'm done. – cat Dec 25 '15 at 00:49
  • Y'all have a /bin/shutdown as a symlink that points to /bin/systemctl right? – cat Dec 25 '15 at 00:54
  • Does the script need to wait for apt to exit, or can it merely wait for the user to try to shutdown again? – cat Dec 25 '15 at 01:21
  • Well, this script is less simple than I thought. – cat Dec 25 '15 at 01:46
  • @LovesTha hopefully, my answer suffices :) – cat Dec 25 '15 at 02:06
  • It's been a while, but if anyone is interested - I've updated my script and instructions. It now uses native dbus method to cancel shutdown action instead of xdotool – Sergiy Kolodyazhnyy Mar 18 '16 at 18:01

3 Answers3

8

Molly-Guard is a program for exactly this purpose; it requires you do a small amount of setup, and have /usr/sbin before /sbin in your $PATH.

Otherwise, according to this the exact details are highly dependent on the GUI / DE's implementation. Since we know your friend is using Xubuntu, this narrows it, but without recompiling Xfce with this support built-in (which would create further issues) it seems very hard.

According to my bountiful research, you can theoretically just replace /sbin/shutdown with a script that checks if an apt job is up and executes sudo shutdown -c or sudo init 2 to cancel a running shutdown and wait for it to exit, but I'm not sure how robust this is.

According to this, you could just make it hard for the user to shutdown, instead of hooking a script.

Finally, as outlined here, you could install unattended-upgrades over whatever system you're using for autoupdates now, and make sure it exits before shutdown as detailed in this answer.


There are many options, all of which are varying levels of unreliable, but I think the best one, which solves what I think is, to some extent, an underlying X / Y Problem at play here, is this:

Use crontab to make his computer run dpkg --configure -a on every boot.

@LovesTha: For your purpose, I recommend unattended-upgrades, or perhaps Molly-Guard.

cat
  • 1,672
  • 1
    Molly guard isn't all that awesome for me, I normally turn this box off by pressing the power button. unattended-upgrade options look reasonable, hopefully it works. – LovesTha Jan 01 '16 at 03:37
7

Introduction

The script bellow uses interrupt-driven polling for specific messages from dbus, and whenever it sees request for shutdown/reboot , it will test whether or not a package manager such as dpkg or apt are running. If they are running, the shutdown request will be cancelled.

Set up

Since you have mentioned that your friend doesn't want to touch command line, you will either need to ssh into his machine, or come over and install this manually.

Manual setup

  1. mkdir $HOME/bin
  2. Copy the script source, save into file named preventShutdown.sh
  3. The script must be executable. Use chmod +x $HOME/bin/preventShutdown.sh to do that
  4. Add a script to list of routines to be ran on login to Unity/Gnome using Startup Applications app or by manually placing a .desktop file into $HOME/.config/autostart

Alternative Setup

sudo apt-get install git
cd /opt
sudo git clone https://github.com/SergKolo/sergrep.git
sudo chmod +x /opt/sergrep/*

Add the script as a startup application.

Script Source

#! /bin/bash

##########################
# AUTHOR: Serg Kolo 
# Date: Saturday, December 26th, 2015
# Description: Script to notify user and prevent 
#   shutdown or reboot
#   if any update or package manager
#   are running. 
# TESTED ON: 14.04.3 LTS, Trusty Tahr
# WRITTEN FOR: http://askubuntu.com/q/702156/295286
# VERSION: 2, removed xdotool, using dbus method
#          changed to C-style of organizing code
#########################

# Copyright (c) 2015 Serg Kolo
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal in 
# the Software without restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 
# the Software, and to permit persons to whom the Software is furnished to do so, 
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all 
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# Uncomment the line bellow if needed for debugging
# set -x
###########################
# VARIABLES
###########################

DISPLAY=:0 # has to be set since we are using notify-send


###########################
# MAIN
###########################
#
#    Basic idea : This runs dbus-monitor which waits for
# "RebootRequested" memberf from com.canonical.Unity.Session ,
# which apprears only when the user clicks the shutdown option 
# from the Unity's top right drop down box. Why RebootRequested ?
# Because this message is guaranteed to pop up once user presses
# the shutdown button.
#   The while loop with read command does the big job.
# dbus-monitor sends initial message , so we want to filter only
# The output that contains the string we need, hence the case...esac
# structure employed here. Once we get the proper message.
# we check whether update-manager or package managers are running
# If there is one instance, then call CancelAction method
# and send notification to the user.
#   Both dbus-monitor and while loop run continuously. This
# can be launcher as script in `/etc/rc.local` or `/etc/rc2.d`
# or preferably (!) in `/etc/xdg/autostart/` . 
#   Here is sample /etc/xdg/autostart/preventShutdown.desktop file
# 
# [Desktop Entry]
# Type=Application
# Name=Prevent-Update
# Exec=/home/$USER/bin/preventShutdown.sh
# OnlyShowIn=GNOME;Unity;
# Terminal=false
# 
# Remember to make this file  as well as script be root-owned with 
# chmod +x /path/to/Script.
# It is preferred to store the script in user's personal $HOME/bin
# folder.
# Make sure to edit $HOME/.profile file to include that into $PATH
# variable

interupt()
{
 qdbus com.canonical.Unity /com/canonical/Unity/Session com.canonical.Unity.Session.CancelAction
 notify-send "<<< UPDATE IN PROGRESS; DO NOT SHUT DOWN>>>"
 wall <<< "<<< UPDATE IN PROGRESS; DO NOT SHUT DOWN>>>"
}

main()
{
 dbus-monitor --profile "interface='com.canonical.Unity.Session',type=signal" |
 while read -r line;
 do
  case "$line" in
   *RebootRequested*)
       pgrep update-manager || pgrep apt-get || pgrep dpkg
    if [ $? -eq 0 ]; then
           interupt
        fi
     ;;
   esac
 done
}

main
muru
  • 197,895
  • 55
  • 485
  • 740
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • +1: This is exactly what I was gonna do in a script but I quickly realised it was far more complex than I originally suspected. – cat Dec 27 '15 at 23:13
5
  1. To quote Einstein:

    Only two things are infinite, the universe and human stupidity, 
    and I'm not sure about the former.
    

    so there is no 100% warranty against human stupidity, but you can make it harder for not-Einsteins to break things by:

  2. Activating automatic updates on shut down.

  3. Explain that computers are not hammers, nor nails but fragile, intelligent pieces of equipment that need two kinds of food: electricity and updates.

Alternatively,

  1. Stop automatic updates altogether and start by visiting your friend more often and install the updates for him/her yourself.
  2. Ask for beers or a nice meal to "tune" the computer

Alternatively:
• use Remmina to keep stuff running smoothly

Fabby
  • 34,259
  • 2
    100% agreed. Also he could set up ssh and port forwarding on the router to manage it remotely. – Sergiy Kolodyazhnyy Dec 26 '15 at 11:38
  • 2
    Less sexism would be nice. – LovesTha Jan 01 '16 at 03:19
  • @LovesTha what sexism? – Fabby Mar 20 '18 at 14:19
  • 8 hours after I left that comment you edited the post to be a lot less sexist. Now over two years later you are having trouble recalling what you originally wrote. – LovesTha Mar 22 '18 at 11:30
  • @LovesTha Yeah, not sexist any more... Time to remove comment. :-) – Fabby Mar 25 '18 at 19:11
  • My guess is Einstein also had enough self-awareness and humility to include himself in the scope of that statement—that being, the stupidity of humanity as a whole. On an individual level, though, no one person is an expert in all domains, so a little more empathy is warranted. – Kevin E Dec 26 '22 at 23:15