Unix HOWTOs and Tips Short unix command line administration tips and scripts

11Jul/140

Execute identical commands in all running OpenVZ containers on a hardware host

Suppose, that you have to execute some identical commands on all your running OpenVZ containers. For example you may want to check their disk usage, or ban an IP on all of them, or force a restart of their webserver for some reason. Typing the same commands over and over again is tiresome, and error prone. Suppose also, that you do not have an automated system like Ansible already (which by the way, I highly recommend) ...

Do not worry, just follow these steps:

1) Save the following script as allvpses_do, somewhere on your PATH, and make it executable:

#!/bin/bash

THECMD=$*

for i in `vzlist -o veid -H|  xargs`; do
   echo "============================  VPS: $i: CMD: $THECMD ===================================";
   echo "$THECMD" | vzctl exec $i - | grep --line-buffered -v 'Executing command:' | sed -e "s@^@VPS $i: @gm"
   echo;
done

2) Run a test command like this:

allvpses_do uptime

... or, you can try:

allvpses_do 'iptables --list -nv|grep DROP'

3) Enjoy your puppet master powers :-)