Advertisement
En808

Proxmox Container Update Script

Mar 13th, 2025
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.54 KB | None | 0 0
  1. #!/bin/bash
  2. # update all containers with colored output
  3.  
  4. # Define colors
  5. GREEN='\033[0;32m'
  6. BLUE='\033[0;34m'
  7. YELLOW='\033[0;33m'
  8. RED='\033[0;31m'
  9. NC='\033[0m' # No Color
  10.  
  11. # list of container ids
  12. containers=$(pct list | tail -n +2 | cut -f1 -d' ')
  13.  
  14. function update_container() {
  15.   container=$1
  16.   echo -e "${BLUE}[Info] Updating container $container${NC}"
  17.   # to chain commands within one exec we will need to wrap them in bash
  18.   pct exec $container -- bash -c "apt update && apt full-upgrade -y && apt autoremove --purge>
  19.  if [ $? -eq 0 ]; then
  20.    echo -e "${GREEN}[Success] Container $container updated successfully${NC}"
  21.  else
  22.    echo -e "${RED}[Error] Failed to update container $container${NC}"
  23.  fi
  24. }
  25.  
  26. for container in $containers
  27. do
  28.  status=`pct status $container`
  29.  if [ "$status" == "status: stopped" ]; then
  30.    echo -e "${YELLOW}[Info] Starting container $container${NC}"
  31.    pct start $container
  32.    if [ $? -ne 0 ]; then
  33.      echo -e "${RED}[Error] Failed to start container $container, skipping${NC}"
  34.      continue
  35.    fi
  36.    echo -e "${YELLOW}[Info] Sleeping 5 seconds to allow container to initialize${NC}"
  37.    sleep 5
  38.    update_container $container
  39.    echo -e "${YELLOW}[Info] Shutting down container $container${NC}"
  40.    pct shutdown $container &
  41.  elif [ "$status" == "status: running" ]; then
  42.    update_container $container
  43.  else
  44.    echo -e "${RED}[Warning] Container $container is in an unknown state: $status${NC}"
  45.  fi
  46. done
  47. wait
  48.  
  49. echo -e "${GREEN}[Complete] All container updates finished${NC}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement