bashaptdpkg

How to automate prompts during apt upgrades in bash?


I am running several Raspberry Pi 4 on Bookworm. On these, I have a bash file as follows:

sudo apt -y update
read -t 600 -p "Pausing 10 minutes between Update and Upgrade"
sudo apt -y upgrade
read -t 600 -p "Pausing 10 minutes between Upgrade and AutoRemove"
sudo apt -y autoremove

Sometimes I get the below screen:

Terminal window with a curses-style interface showing a dialog labeled "Modified configuration file" asking whether changes should be retained or replaced

I also run this as a cron job (once a quarter) that calls the above script, but it fails when run via the cron. How can I recognize that this GUI prompt is up and input the right answer?

Important Note: This doesn't happen always, so I want it to recognize when it happens and only then provide the correct input.


Solution

  • For the specific case of dpkg frontends such as apt, it's built to be able to disable interactive prompts altogether -- you don't need to automate the prompts, you can just turn them off.

    Export the environment variable DEBIAN_FRONTEND=noninteractive while you run apt, and you'll get no more interactive prompts; if you need to assert specific settings, you can set them noninteractively with utilities from debconf-utils -- see its documentation.

    Concretely:

    sudo DEBIAN_FRONTEND=noninteractive apt -y update
    read -t 600 -p "Pausing 10 minutes between Update and Upgrade"
    sudo DEBIAN_FRONTEND=noninteractive apt -y upgrade
    read -t 600 -p "Pausing 10 minutes between Upgrade and AutoRemove"
    sudo DEBIAN_FRONTEND=noninteractive apt -y autoremove