Here is a compilation of working one-liners as of September 2020. Thanks to all the previous authors for inspiration.
Halt all vagrant boxes
Version 1 (shorter)
vagrant global-status | awk '/virtualbox running/{ print $1 }' | xargs vagrant halt
Version 2 (more robust)
macOS/BSD compatible:
vagrant global-status --machine-readable | cut -d, -f5 | grep -B3 running | egrep -o '[0-9a-f]{7}' | xargs -n 1 -- vagrant halt
GNU compaible:
vagrant global-status --machine-readable | cut -d, -f5 | grep -B3 running | egrep -o '[0-9a-f]{7}' | xargs -r -d '\n' -n 1 -- vagrant halt
Alias
You can add this to an alias for quick execution. Add this code to ~/.bashrc
, ~/.zshrc
or your shell's equivalent.
macOS/BSD example:
alias vagrantHaltAll="vagrant global-status --machine-readable | cut -d, -f5 | grep -B3 running | egrep -o '[0-9a-f]{7}' | xargs -n 1 -- vagrant halt"
# Usage:
vagrantHaltAll
Suspend all vagrant boxes
Or if you want to suspend
any running machines, the syntax is easy:
vagrant suspend --all
I recommend using halt
for web servers; it saves disk space and may be faster.