fork stop container

This commit is contained in:
2024-01-28 20:50:55 +01:00
parent 02bbcc3878
commit 1fb20e7f88
7 changed files with 835 additions and 39 deletions

42
update-lxc-container.sh Executable file
View File

@@ -0,0 +1,42 @@
#!/bin/bash
declare -A pids
BRed='\033[1;31m' # Red
BGreen='\033[1;32m' # Green
Color_Off='\033[0m' # Text Reset
function stop_container() {
lxc stop ${1}
echo -e $(date) "- ${BGreen}Container ${1} stopped${Color_Off}"
}
echo $(date) "- Start updating LXC container..."
for i in $(lxc list type=container -c ns --format csv); do
cont_name=${i%,*}
echo $(date) "- Updating ${cont_name} container..."
if [ "${i#*,}" == "STOPPED" ]; then
echo -e $(date) "- ${BGreen}Start container ${cont_name}${Color_Off}"
lxc start ${cont_name}
sleep 5
else
echo -e $(date) "- ${BGreen}Container ${cont_name} is already started${Color_Off}"
fi
lxc exec ${cont_name} -- sh -c "apt update"
sleep 2
lxc exec ${cont_name} -- sh -c "apt upgrade -y"
sleep 2
lxc exec ${cont_name} -- sh -c "apt autoremove"
sleep 2
if [ "${i#*,}" == "STOPPED" ]; then
echo $(date) "- Stop container ${cont_name}"
stop_container ${cont_name} &
pids[${cont_name}]=$!
fi
sleep 2
done
for pid in ${pids[@]}; do
wait $pid
done
echo $(date) "- Updating LXC container ended"