Quantcast
Channel: Debian User Forums
Viewing all articles
Browse latest Browse all 3424

System and Network configuration • Automatically fix broken apt system

$
0
0
I'm looking for comments on a hack I've just written to make any Debian system auto-magically recover from apt breakage.

Use case: you have just installed a Debian-based distribution (say, Mint / LMDE) on someone's computer. The owner is not tech-savvy. Some day, apt will break because of a power outage while updating, a forced shutdown or whatever (in my experience, this happens sooner than later). Updates become impossible, installing applications either: the update manager or application manager alike throw an error that recommends opening a terminal and launching "dpkg --configure -a" (or some other technical message). The (non tech-savvy) user won't do that.

This hack is triggered at boot, waits for a working internet connection, tests if apt is broken and tries to fix it.

The main script: fix-apt.sh

Code:

#!/bin/bashuntil [[ `ping -c 1 archive.org` ]] || [[ `ping -c 1 kernel.org` ]] || [[ `ping -c 1 google.com` ]]; do    sleep 10done# Tests wether dpkg -C reports something to do OR apt-get install yields an errorif [[ $(dpkg -C) ]] || ! apt-get install hostname -y; then    apt-get -y clean    apt-get -y update    dpkg --configure -a --force-confdef --force-confnew    apt-get install -f -y    apt autoremove --purge -yfi
Systemd service configuration file: fix-apt.service

Code:

[Unit]Description=Fix broken APT if needed[Service]ExecStart=/bin/fix-apt.sh[Install]WantedBy=multi-user.target
The script to install the service: install-fix-apt-on-boot.sh
(All three files should be located in the same folder.)

Code:

#!/bin/bashsudo cp fix-apt.service /etc/systemd/systemsudo cp fix-apt.sh /binsudo systemctl enable fix-apt.service
I'm asking for comments because of many doubts!
  • Why, in the first place, is apt so vulnerable?
  • Why do Debian and Debian-based distributions rely on a terminal-based solution to a broken apt system, that non-tech-savvy users won't be able to do?
  • Is it enough to test for a broken apt system like I do?
  • Does my script fix *any* situation where the apt system is broken (excluding the user has wrongly fiddled with the system, of course)?
  • Would my script do more harm than good?
Thanks for your feedback!

Statistics: Posted by ricflomag — 2025-01-09 00:25 — Replies 4 — Views 141



Viewing all articles
Browse latest Browse all 3424

Trending Articles