Update all LXC with one simple script

sshanee

Member
Oct 6, 2019
11
6
8
40
Simple bash script to APT update all LXC containers which are stopped or running status

Will start stopped containers to update then shut them down in the background and move on to next container

Code:
#!/bin/bash
# update all containers

# list of container ids we need to iterate through
containers=$(pct list | tail -n +2 | cut -f1 -d' ')

function update_container() {
  container=$1
  echo "[Info] Updating $container"
  # to chain commands within one exec we will need to wrap them in bash
  pct exec $container -- bash -c "apt update && apt upgrade -y && apt autoremove -y"
}

for container in $containers
do
  status=`pct status $container`
  if [ "$status" == "status: stopped" ]; then
    echo [Info] Starting $container
    pct start $container
    echo [Info] Sleeping 5 seconds
    sleep 5
    update_container $container
    echo [Info] Shutting down $container
    pct shutdown $container &
  elif [ "$status" == "status: running" ]; then
    update_container $container
  fi
done; wait
 
Just curious, as I stumbled upon this, as 'apt' replaced 'apt-get'... shouldn't 'apt upgrade' in the above script be the equivalent of the previous recommended 'apt-get dist-upgrade', i.e the script should be 'apt full-upgrade' (the previous apt-get dist-upgrade) to avoid dependency issues?
 
Just curious, as I stumbled upon this, as 'apt' replaced 'apt-get'... shouldn't 'apt upgrade' in the above script be the equivalent of the previous recommended 'apt-get dist-upgrade', i.e the script should be 'apt full-upgrade' (the previous apt-get dist-upgrade) to avoid dependency issues?

Sorry for the late reply, i didnt notice this comment. But.. thats above my paygrade. Im not really sure what you mean, should i change my script?
 
Three lines without counting shebang :D

Code:
#!/bin/bash

for CT in $(pct list | grep running | awk '{print $1}'); do
  pct exec ${CT} -- bash -c "apt update && apt dist-upgrade -y && apt clean"
done
 
Last edited:
  • Like
Reactions: Helmut101
Three lines without counting shebang :D

Code:
#!/bin/bash

for CT in $(pct list | grep running | awk '{print $1}'); do
  pct exec ${CT} -- bash -c "apt update && apt dist-upgrade -y && apt clean"
done
While i appreciate learning and beautifying code, this isnt nearly as robust. It doesnt start stopped containers or give any information. Your 3 lines does not replace what this script does.
 
While i appreciate learning and beautifying code, this isnt nearly as robust. It doesnt start stopped containers or give any information. Your 3 lines does not replace what this script does.
Well, it is supposed that if container is stopped, doesn't need to be upgraded or is not in production. And by the way, this an example of a short script to do the job, can be improved, and adapted to your needs... But thanks pointing out that.

Regards!
 
Small change to see the name of the container and also do full-upgrade :

Code:
#!/bin/bash
# update all containers

# list of container ids we need to iterate through
containers=$(pct list | tail -n +2 | cut -f1 -d' ')

function update_container() {
  container=$1
  name=`pct exec $container cat /etc/hostname`
  echo "[Info] Updating $container : $name"
  # to chain commands within one exec we will need to wrap them in bash
  pct exec $container -- bash -c "apt update && apt upgrade -y && apt full-upgrade -y && apt autoremove -y"
}

for container in $containers
do
  status=`pct status $container`
  if [ "$status" == "status: stopped" ]; then
    echo [Info] Starting $container
    pct start $container
    echo [Info] Sleeping 5 seconds
    sleep 5
    update_container $container
    echo [Info] Shutting down $container
    pct shutdown $container &
  elif [ "$status" == "status: running" ]; then
    update_container $container
  fi
done; wait
 
  • Like
Reactions: sastromo and Uruk
First, I'd also add apt clean and a --purge on the autoremove.

Second, why don't you use unattended or some other form of automation tool and deceided to reinvent the wheel once more? e.g. unattended is already built for exactly this and available in Debian/Ubuntu including doing it in a way that you don't need any interaction.
 
  • Like
Reactions: modem7
Hi @ll,
lxd-tools didn't realy work with proxmox.

I Implement logging and exit tracking.
Also changed some formats.

In next step, I will make it workable for the hole Cluster, if available.

Here you get ;)

https://github.com/BassT23/LXC-Update

Update:
v2.0.1 is coming, with Cluster Mode
 
Last edited:
  • Like
Reactions: avluis86

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!