
我有一個用於安裝伺服器的無人值守腳本。在腳本的開頭有一個sudo apt-get dist-upgrade --yes
. dist 升級的最後有一個令人討厭的用戶輸入螢幕,要求重新啟動服務:
是否可以自動接受服務重新啟動或停用此畫面?它破壞了我的整個劇本。另外我擔心它可能會讓我的伺服器在更新時卡在某個時刻...
與 apt-get Upgrade 的結果相同
編輯:我嘗試過但沒有成功:
#!/bin/bash
sudo apt-get update
sudo apt-get remove apt-listchanges --assume-yes --force-yes &&
#using export is important since some of the commands in the script will fire in a subshell
export DEBIAN_FRONTEND=noninteractive &&
export APT_LISTCHANGES_FRONTEND=none &&
#lib6c was an issue for me as it ignored the DEBIAN_FRONTEND environment variable and fired a prompt anyway. This should fix it
echo 'libc6 libraries/restart-without-asking boolean true' | debconf-set-selections &&
echo "executing wheezy to jessie" &&
find /etc/apt -name "*.list" | xargs sed -i '/^deb/s/wheezy/jessie/g' &&
echo "executing autoremove" &&
sudo apt-get -fuy --force-yes autoremove &&
echo "executing clean" &&
sudo apt-get --force-yes clean &&
echo "executing update" &&
sudo apt-get update &&
echo "executing upgrade" &&
sudo apt-get --force-yes -o Dpkg::Options::="--force-confold" --force-yes -o Dpkg::Options::="--force-confdef" -fuyq upgrade &&
echo "executing dist-upgrade" &&
sudo apt-get --force-yes -o Dpkg::Options::="--force-confold" --force-yes -o Dpkg::Options::="--force-confdef" -fuyq dist-upgrade
答案1
其他答案完全跳過needrestart
。
但是環境變數NEEDRESTART_MODE
允許指定一種模式。透過選擇“(a)自動”,您可以受益,needrestart
而不會被提示阻止:
sudo NEEDRESTART_MODE=a apt-get dist-upgrade --yes
答案2
正如其他人所提到的,這種情況下的問題在於命令needrestart
,它是 Ubuntu 中 apt-get 升級過程的一部分(特別是我正在使用的 22.04)。預設情況下,它設定為“互動”模式,這會導致腳本中斷。
要更改此行為,我們可以編輯該/etc/needrestart/needrestart.conf
文件,更改以下行:
#$nrconf{restart} = 'i';
到
$nrconf{restart} = 'a';
(如果我們想自動重新啟動服務)或
$nrconf{restart} = 'l';
只是列出需要重新啟動的服務。
如果您正在執行腳本並希望在不使用 vim 等互動式編輯器的情況下進行此編輯,您可以使用 來執行此操作sed
,例如:
sudo sed -i 's/#$nrconf{restart} = '"'"'i'"'"';/$nrconf{restart} = '"'"'a'"'"';/g' /etc/needrestart/needrestart.conf
由於設定檔中的單引號以及 sed 處理單引號的方式,它看起來很難看,但它確實有效。如果您有更好看的方法,請發表評論。
答案3
不要使用 sed 或編輯主配置,而是在 /etc/needrestart/conf.d/no-prompt.conf 中加入您自己的設定檔:
$nrconf{restart} = 'a';
答案4
或者,我思考你可以刪除needrestart
包裝本身。
sudo apt-get remove needrestart
我將其用於 Ubuntu 22.04 上的 AWS EC2 配置。無論出於何種原因,export DEBIAN_FRONTEND=noninteractive
似乎都不起作用。