Quick and dirty bash script to apt-get update all OpenVZ containers
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!