Skip to content

Instantly share code, notes, and snippets.

@grega
Last active January 26, 2019 16:27
Show Gist options
  • Save grega/61355d041ccfaef66419 to your computer and use it in GitHub Desktop.
Save grega/61355d041ccfaef66419 to your computer and use it in GitHub Desktop.
Output the total number of currently running VirtualBox VMs, along with their names. This has been written with Vagrant in mind, where VMs are named `hostname_default_xxxxxxxxx` where only the value of `hostname` is relevant/recognisable.
#!/bin/bash
machines=()
for machine in `VBoxManage list runningvms|cut -d" " -f 1`; do
machines+=("$machine")
done
if [ ${#machines[@]} -eq 1 ]; then
machinename=$(echo ${machines[@]} | cut -d'_' -f 1)
echo "1 VM running: $machinename"
elif [ ${#machines[@]} -gt 1 ]; then
echo "${#machines[@]} VMs running:"
for machine in ${machines[@]}; do
machinename=$(echo ${machine} | cut -d'_' -f 1)
echo "$machinename\""
done
else
echo "No VMs running"
fi
@grega
Copy link
Author

grega commented Jun 17, 2014

To output the VBox status in terminal / ZSH (in the bottom right of the shell), add the following function to your theme-name.zsh-theme:

## Output VBox status
prompt_vm() {
  function vbox_status {
    echo `~/Vagrant/vbox-status-check.sh`
  }

  screen_w=$(tput cols)   # Get screen width
  screen_h=$(tput lines)  # Get screen height
  str=$(vbox_status)      # String to put in corner
  string_w=${#str}
  let "x = $screen_w - $string_w"

  tput sc               # Save current position
  tput cup $screen_h $x # Move to corner
  echo -ne $str         # Put string in the corner
  tput rc               # Go back to saved position
}

(see: http://stackoverflow.com/questions/24274456/zsh-sticky-prompt)

Then just call the function in build_prompt() like so:

build_prompt() {
  RETVAL=$?
  prompt_status
  etc...
  prompt_vm
  prompt_end
}

I find this particularly handy in avoiding the case where I have a VM or two left running in the background unused and draining my battery!

Example:

No VMs running: https://www.evernote.com/shard/s153/sh/6bc2d85e-d9b7-4a69-8cf5-b831b0bd90ec/680ec902d7d540f94ace5aff5d80f6f6/res/5b3085c0-f003-4d26-ac9c-2fbe4d4ecccf/skitch.png?resizeSmall&width=832

1 VM running: https://www.evernote.com/shard/s153/sh/cf208c18-9d87-47d0-afb2-4b19c28662b8/8758ef7286dfc035381ab1e2f5d3c28f/res/8a47b88d-cb78-4519-9857-040a7c93e91c/skitch.png?resizeSmall&width=832

@jcreasey
Copy link

Great job. I tried to get it to erase the prompt so that it wouldn't track up the terminal after you reach the bottom. On my mac it didn't support the erase commands though unfortunately.

@grega
Copy link
Author

grega commented Jun 18, 2014

Thanks for the tput related help @jcreasey!

@grega
Copy link
Author

grega commented Jun 18, 2014

I'll soon be looking at implementing this alongside Vagrant 1.6's 'global status' feature (rather than relying on VBox): http://www.vagrantup.com/blog/feature-preview-vagrant-1-6-global-status.html

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