Skip to content

Instantly share code, notes, and snippets.

@darkwizard242
Last active April 5, 2020 03:44
Show Gist options
  • Save darkwizard242/2c23ed72b387eb4ceb255a4e43af7c42 to your computer and use it in GitHub Desktop.
Save darkwizard242/2c23ed72b387eb4ceb255a4e43af7c42 to your computer and use it in GitHub Desktop.
Shell script to stop all running vagrant instances under current user.
#!/bin/bash -e
### 1. Script globally checks for all vagrant instances running under the user executing the script.
### 2. Assigns the instance ID's of any running instance to the variable "instances".
### 3. Checks for whether the variable "instances" is an empty string.
### 4. If not an empty string, then run a for loop to print vagrant instance information and halts (stops) it.
vagrant_stop () {
instances=$(vagrant global-status --prune | grep running | awk '{ print $1}')
if [ -z "${instances}" ];
then
echo -e "\nVagrant instances are not running. No instance to halt!\n"
else
for instance in ${instances};
do
echo -e "\nVagrant instance state for:\t${instance}\n"
vagrant status ${instance}
echo -e "\nStopping Vagrant instance:\t${instance}\n"
vagrant halt ${instance}
echo -e "____________________________________________________________"
done
fi
}
vagrant_stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment