当前位置: 首页>>技术教程>>正文


apt – 防止 Ubuntu 在后台自动更新完成之前关闭

, , , ,

问题描述

我在朋友的 PC 上安装了 (x)Ubuntu 14.04。自动更新设置为“下载并自动安装更新”。

问题是,在使用几个月后,他在软件包升级完成之前不知不觉地关闭了他的 PC。这会导致依赖项/包损坏,从而导致更新受到影响并且需要运行 sudo dpkg --configure -a

是否可以像 Windows 那样让 Ubuntu 在 PC 关闭或重新启动之前等待更新完成,以确保永远不会有损坏的软件包并且他的 PC 将保持自动更新?

最佳回答

Molly-Guard 正是为此目的而编写的程序;它需要您进行少量设置,并在 $PATH 中的 /sbin 之前有 /usr/sbin

否则,根据 this,确切的细节高度依赖于 GUI/DE 的实现。由于我们知道您的朋友正在使用 Xubuntu,因此它缩小了范围,但是如果不使用此支持重新编译 Xfce 内置(这会产生更多问题),这似乎很难。

根据我的大量研究,理论上您可以用一个脚本替换 /sbin/shutdown,该脚本检查 apt 作业是否启动并执行 sudo shutdown -csudo init 2 以取消正在运行的关机和 wait 以使其退出,但我不确定这有多健壮.

根据 this ,您可以让用户难以关闭,而不是挂钩脚本。

最后,如 here 所述,您可以将 unattended-upgrades 安装在您现在用于自动更新的任何系统上,并确保它在关闭之前以 detailed in this answer 的形式退出。


有很多选择,所有这些都不同程度的不可靠,但我认为最好的一个,它解决了我认为在某种程度上在这里起作用的潜在 X / Y Problem,是这样的:

使用 crontab 使他的计算机在每次启动时都运行 dpkg --configure -a

@LovesTha:为了你的目的,我推荐 unattended-upgrades ,或者 Molly-Guard。

次佳回答

Introduction

下面的脚本使用 interrupt-driven 轮询来自 dbus 的特定消息,并且每当它看到关闭/重新启动的请求时,它将测试是否有诸如 dpkgapt 之类的包管理器正在运行。如果它们正在运行,则关闭请求将被取消。

设置

既然你提到你的朋友不想接触命令行,你要么需要 ssh 进入他的机器,要么过来手动安装。

手动设置

  1. mkdir $HOME/bin

  2. 复制脚本源代码,保存到名为 preventShutdown.sh 的文件中

  3. 脚本必须是可执行的。使用 chmod +x $HOME/bin/preventShutdown.sh 来做到这一点

  4. 使用启动应用程序应用程序或通过手动将 .desktop 文件放入 $HOME/.config/autostart 将脚本添加到要在登录 Unity/Gnome 时运行的例程列表

替代设置

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

将脚本添加为启动应用程序。

脚本源

#! /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

第三种回答

  1. 引用爱因斯坦的话:

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

    所以没有 100% 保证人类的愚蠢,但是你可以通过以下方式让 not-Einsteins 更难破解:

  2. Activating automatic updates on shut down

  3. 说明计算机不是锤子,也不是钉子,而是脆弱的智能设备,需要两种食物:电力和更新。

或者,

  1. 完全停止自动更新并开始更频繁地访问您的朋友并自己为他/她安装更新。

  2. 向电脑”tune”要啤酒或一顿美餐

或者: • 使用 Remmina 保持工作顺利进行

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/12231.html,未经允许,请勿转载。