Skip to content

Instantly share code, notes, and snippets.

@colby
Last active August 29, 2015 14:18
Show Gist options
  • Save colby/f98cbc2380db0be48bbb to your computer and use it in GitHub Desktop.
Save colby/f98cbc2380db0be48bbb to your computer and use it in GitHub Desktop.
Sandman: A bash script to find all known Vagrant VMs and put them to sleeps. Zzz.
#!/usr/bin/env bash
# variables
vagrant=""
# functions
errcho() {
>&2 echo "$*"
exit 1
}
check() {
which "$1" || errcho "Missing dependency: $1"
}
# deps
vagrant=$(check vagrant)
# main
printf "Putting VMs to bed. "
while read line
do
set -- $line
# 1:a8533bc 2:utility 3:virtualbox 4:poweroff 5:/Users/colbyolson/src/Work/misc/chef
[ "$4" = 'running' ] && $vagrant halt $1
printf "z"
done < <($vagrant global-status --prune)
printf "\n"
@colby
Copy link
Author

colby commented Apr 1, 2015

INSTALL

$ curl -so /usr/local/bin/sandman https://gist.githubusercontent.com/colby/f98cbc2380db0be48bbb/raw/0b21b0c3580ded26412bcf2b00f32a4d7fd6f9ef/sandman
$ chmod +x /usr/local/bin/sandman

HOWTO

Vagrant now supports global-status, which lists out all known VMs. I've appended --prune to the command so that older or stale VMs are removed.

$ sandman

UNINSTALL

$ rm /usr/local/bin/sandman

@aedmonds
Copy link

aedmonds commented May 1, 2015

Works swell. I think adding an option to specify either halt or `suspend would be helpful. I'll see if I can add it somehow but I'm not making any promises...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment