Update all LXC with one simple script

sshanee

New Member
Oct 6, 2019
8
3
3
37
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
 
Feb 11, 2019
15
0
6
39
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?
 

sshanee

New Member
Oct 6, 2019
8
3
3
37
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?
 

Koratsuki

Member
Dec 15, 2014
3
1
21
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

sshanee

New Member
Oct 6, 2019
8
3
3
37
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.
 

Koratsuki

Member
Dec 15, 2014
3
1
21
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!
 

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 your own in 60 seconds.

Buy now!