User Tools

Site Tools


Sidebar

js#vista.png msort nsort

virtualization:containers:execute-command-in-all-containers

Executing a command in all containers

OpenVZ

If you wish to execute a command in all running OpenVZ Containers, you can use the following command:

for i in `cat /proc/vz/veinfo | awk '{print $1}'|egrep -v '^0$'`; \
do echo "Container $i"; vzctl exec $i <command>; done

where <command> is the command to be executed in all the running Containers. For example:

for i in `cat /proc/vz/veinfo | awk '{print $1}'|egrep -v '^0$'`; \
do echo "Container $i"; vzctl exec $i 'apt-get update; apt-get -y upgrade'; done

LXC

If you wish to execute a command in all running LXC Containers, you can use the following command:

for i in `lxc-ls -1 --active | awk '{print $1}'|egrep -v '^0$'`; \
do echo "Container $i"; pct exec $i <command>; done

where <command> is the command to be executed in all the running Containers. For example:

for i in `lxc-ls -1 --active | awk '{print $1}'|egrep -v '^0$'`; \
do echo "Container $i"; pct exec $i /usr/bin/curl -- -s https://packages.wazuh.com/key/GPG-KEY-WAZUH | apt-key add -- -; done

* Be advised that in certain situations you will need to add “--” prior to options that are passed to commands in the container. Otherwise the “pct” utility will interpret the option as an option meant for it and not the command being executed in the container. Now we all know this problem could be easily remedied if “pct” were to allow for quoted commands but then you would need escapes at time as well.

virtualization/containers/execute-command-in-all-containers.txt · Last modified: 2020/02/24 11:16 (external edit)