Quickly stopping all running vagrant instances
Problem Statement
When running several vagrant instances, it can be slightly time consuming to have to individually stop all of them.
Solution
The snippet below is a bash script that can efficiently stop all running vagrant instances under the user running the script.
Note: This script works well on both Linux/Unix and MacOS operating systems.
#!/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
Alternatively, you can also utilize the following one-liner to take care of it for you immediately.
curl -fsSL https://gist.githubusercontent.com/darkwizard242/2c23ed72b387eb4ceb255a4e43af7c42/raw/5cc36730599e9ad754ea46f6e154fc0ab260b20b/vagrant_stop.sh | bash
Sample Outputs
- Following is an example of the execution when none of the vagrant instances are running.
- Following is an example of the execution when one or more vagrant instances are running.
If this post and snippet has been of any help to you or if you liked it, please don’t hesitate to click the “⭐” button for the gist here. 😊
comments powered by Disqus