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

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

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

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

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

答案1

莫莉卫士是一个专门用于此目的的程序;它需要你进行少量的设置,并且/usr/sbin /sbin在你的$PATH

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

根据我的大量研究,你可以理论上只需/sbin/shutdown用一个脚本替换,该脚本检查适当的作业是否启动并执行sudo shutdown -csudo init 2取消正在运行的关闭并wait使其退出,但我不确定这有多么强大。

根据,你可以让它变得困难用户关闭,而不是挂接脚本。

最后,正如概述的那样这里,您可以unattended-upgrades在现在使用的任何系统上安装自动更新,并确保它在关机前退出在这个答案中有详细说明


有很多选择,但都是不同程度的不可靠,但我认为最好的一个,解决了什么在某种程度上,思考是一种潜在的X / Y问题这里起作用的是:

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

@LovesTha:为了您的目的,我推荐unattended-upgrades,或者也许是 Molly-Guard。

答案2

介绍

下面的脚本使用中断驱动轮询来自 dbus 的特定消息,每当它看到关机/重启请求时,它就会测试诸如dpkg或之类的包管理器是否apt正在运行。如果它们正在运行,则将取消关机请求。

设置

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

手动设置

  1. mkdir $HOME/bin
  2. 复制脚本源,保存到名为preventShutdown.sh
  3. 脚本必须是可执行的。使用chmod +x $HOME/bin/preventShutdown.sh来执行此操作
  4. 使用启动应用程序应用程序或手动将.desktop文件放入到登录 Unity/Gnome 时要运行的例程列表中,添加一个脚本$HOME/.config/autostart

替代设置

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

答案3

  1. 引用爱因斯坦的话:

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

    所以,没有 100% 的保证说人类不会犯愚蠢的错误,但是你可以通过以下方式让非爱因斯坦更难破坏事物:

  2. 关机时激活自动更新

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

或者,

  1. 完全停止自动更新,开始更频繁地拜访你的朋友并亲自为他/她安装更新。
  2. 要求喝啤酒或吃一顿美餐来“调整”电脑

或者:
• 使用 Remmina 确保一切顺利运行

相关内容