Skip to content

Instantly share code, notes, and snippets.

@doug65536
Forked from lcherone/lxc-exec-all.sh
Last active December 14, 2021 15:46
Show Gist options
  • Save doug65536/ce7ba68758bbef582ab7ea2ebdd5715d to your computer and use it in GitHub Desktop.
Save doug65536/ce7ba68758bbef582ab7ea2ebdd5715d to your computer and use it in GitHub Desktop.
LXD run command in all running containers, optionally in parallel
#!/bin/bash
#
# Run command in all running containers
# Usage: $ ./lxc-exec-all.sh apt update && apt upgrade
#
# To run in all in parallel, put '*' as the first argument.
#
if "$1"=="*" then
shift
# Run in parallel
sudo lxc list volatile.last_state.power=RUNNING -c n --format csv | \
parallel -j $(nproc) -- sudo lxc exec '{}' -- "$@" || exit
else
# Sequential
for container in $(sudo lxc list volatile.last_state.power=RUNNING -c n --format csv); do
sudo lxc exec "$container" -- "$@"
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment