當前位置: 首頁>>技術教程>>正文


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/zh-tw/article/12231.html,未經允許,請勿轉載。