Quick and dirty bash script to apt-get update all OpenVZ containers

By , 2011-11-30 22:29

It’s a bit of a pain having to run upgrades on all servers. I could of course, set up unattended upgrades, but I always liked initiating the upgrade process myself. So I wrote a little bash script that will initiate apt-get update and apt-get upgrade on all running OpenVZ containers.

Note that this only works for Debian-based distros. So Debian, *buntu, Linux Mint and the like.

It’s very rough, no error-handling or safeguards, so use at your own risk. Works for me, but YMMV.

#!/bin/bash
#Delete temp file
rm /tmp/tmp-script.sh
#Get running VZ
CTIDS=$(vzlist | awk '{print $1}' | sed -e '/CTID/d' -e ':a;N;$!ba;s/\n/ /g' )
# Echo list of running IDs
echo "$CTIDS"
CTIDarray=($CTIDS)
for x in ${CTIDarray[@]}
do
    echo "#/bin/bash" > /tmp/tmp-script.sh
    chmod +x /tmp/tmp-script.sh
    echo vzctl exec $x "apt-get update &&;  apt-get -y upgrade" >> /tmp/tmp-script.sh
    screen -d -m /tmp/tmp-script.sh
done
#Delete temp file
rm /tmp/tmp-script.sh
#Show running screens
screen -x

First, we rm the /tmp/tmp-script.sh. Starting off with very bad form, I know, feeling lazy right now. Then I use awk and sed to get the IDs of running containers from the output of the vzlist command, and place them on a single line, separated by spaces. Those IDs are than put in an array, so that the update command can be called using a for loop.

For some reason, I couldn’t get screen to launch the

vzctl exec $x "apt-get update &&  apt-get -y upgrade"

command directly, hence the hideous use of a temp file. If anyone can fix/improve this, I would be glad to hear from you!

One Response to “Quick and dirty bash script to apt-get update all OpenVZ containers”

  1. Chromium 77.0.3865.90 Chromium 77.0.3865.90 Ubuntu x64 Ubuntu x64
    Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/77.0.3865.90 Chrome/77.0.3865.90 Safari/537.36

    can it be used for all linux distributions?

Leave a Reply

 

Custom theme by me. Based on Panorama by Themocracy