Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # update all containers with colored output
- # Define colors
- GREEN='\033[0;32m'
- BLUE='\033[0;34m'
- YELLOW='\033[0;33m'
- RED='\033[0;31m'
- NC='\033[0m' # No Color
- # list of container ids
- containers=$(pct list | tail -n +2 | cut -f1 -d' ')
- function update_container() {
- container=$1
- echo -e "${BLUE}[Info] Updating container $container${NC}"
- # to chain commands within one exec we will need to wrap them in bash
- pct exec $container -- bash -c "apt update && apt full-upgrade -y && apt autoremove --purge>
- if [ $? -eq 0 ]; then
- echo -e "${GREEN}[Success] Container $container updated successfully${NC}"
- else
- echo -e "${RED}[Error] Failed to update container $container${NC}"
- fi
- }
- for container in $containers
- do
- status=`pct status $container`
- if [ "$status" == "status: stopped" ]; then
- echo -e "${YELLOW}[Info] Starting container $container${NC}"
- pct start $container
- if [ $? -ne 0 ]; then
- echo -e "${RED}[Error] Failed to start container $container, skipping${NC}"
- continue
- fi
- echo -e "${YELLOW}[Info] Sleeping 5 seconds to allow container to initialize${NC}"
- sleep 5
- update_container $container
- echo -e "${YELLOW}[Info] Shutting down container $container${NC}"
- pct shutdown $container &
- elif [ "$status" == "status: running" ]; then
- update_container $container
- else
- echo -e "${RED}[Warning] Container $container is in an unknown state: $status${NC}"
- fi
- done
- wait
- echo -e "${GREEN}[Complete] All container updates finished${NC}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement