How can I tell if a virtual machine is up from the command line?

How can I tell from the command line if a specific virtual machine is up and running from the command line?

1

7 Answers

vboxmanage list vms

Dos not (anymore?) tells if the vm is runing or not. It list also not running ones.

vboxmanage showvminfo "your_vm_name" | grep -c "running (since"

Will returns 1 if it's running, or 0 if not.

3

If you want a list of all VMs to see if they are running or not use this command:

vboxmanage list vms --long | grep -e "Name:" -e "State:"

This will show the VMs name in one line and its status in the following line such as in

Name: windows10pro
State: running (since 2017-06-09T09:16:46.593000000)
Name: ubuntu16LTS
State: powered off (since 2017-02-09T19:11:33.000000000)
Name: zammad
State: running (since 2017-06-09T09:08:13.871000000)
2

This command outputs the list of running vms (tested on Virtualbox 5.1)

VBoxManage list runningvms

To know if a vm is running, this command should do the job (return 1 if running, 0 otherwise) :

VBoxManage list runningvms | sed -r 's/^"(.*)".*$/\1/' | grep 'VM Name' | wc -l
1

I believe you can get this information using VBoxManage (command-line interface to VirtualBox).

You can use The showvminfo command for show information about a particular virtual machine.

This is the same information as VBoxManage list vms would show for all virtual machines.

0

Combining VBoxManage list runningvms with grep alone without anything else will not only give a console output, but will also provide the return code required for shell scripting needs. The grep command will require the exact double quote for the VM in the case where a vm name is provided: example:

$ VBoxManage list runningvms |grep '"demo_vm"'
$ echo $?
1

For a running one

$ VBoxManage list runningvms |grep '"demo_vm_on"'
"demo_vm_on" {bbff5c0e-f8d4-4751-8d34-c53c4b191613}
$ echo $?
0

In the other answer where sed was used to eliminate the double quotes, or if the grep was done without the double quotes, false positives will be returned. Demonstration:

$ VBoxManage list runningvms |grep demo_vm
"demo_vm_on" {bbff5c0e-f8d4-4751-8d34-c53c4b191613}
$ echo $?
0

root@yourshell#vboxmanage list vms

Also, see this document.

1

Generic ps script:

C:\Program Files\Oracle\VirtualBox> if ( ! ( C:"Program Files"\Oracle\VirtualBox\vboxmanage showvminfo "vmname" | Select-String "running (since" )) { C:"Program Files"\Oracle\VirtualBox\VirtualBoxVM.exe --comment "vmname" --startvm "{vmguid}" }

note: You can get vmguid by right clicking on a vm in virtualbox and creating a shortcut on desktop to start that vm.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like