LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Virtualization and Cloud
User Name
Password
Linux - Virtualization and Cloud This forum is for the discussion of all topics relating to Linux Virtualization and Linux Cloud platforms. Xen, KVM, OpenVZ, VirtualBox, VMware, Linux-VServer and all other Linux Virtualization platforms are welcome. OpenStack, CloudStack, ownCloud, Cloud Foundry, Eucalyptus, Nimbus, OpenNebula and all other Linux Cloud platforms are welcome. Note that questions relating solely to non-Linux OS's should be asked in the General forum.

Notices


Reply
  Search this Thread
Old 11-10-2011, 08:05 AM   #1
el_pedro
LQ Newbie
 
Registered: Nov 2011
Posts: 2

Rep: Reputation: Disabled
How can I use a Bash script to determine if Virtualbox vm is running?


Hi,

I'm very new to bash scripts (and linux in general) but am trying to run a script on a host to determine if any VirtualBox virtualmachines are running.

The relevant part of the script is:
Code:
if [ 'sudo -u virtualboxuser VBoxManage list runningvms | wc -l' != "0" ]
    then
        echo "VirtualMachine is running" >> $logfile
        echo $(sudo -u virtualboxuser VBoxManage list runningvms | wc -l) >> $logfile
fi
Unfortunately, I always seem to enter the 'if' statement even if no virtual machines are running. I'm piping the output to a logfile, and looking at my logfile I can see that the result of the line
Code:
echo $(sudo -u virtualboxuser VBoxManage list runningvms | wc -l) >> $logfile
is 0.

Please can anyone tell me why the 'if' statement is still being executed? Or is there a better way to determine if any virtualmachines are running?
 
Old 11-10-2011, 08:18 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
The problem is in
Code:
if [ 'sudo -u virtualboxuser VBoxManage list runningvms | wc -l' != "0" ]
The single quotes make the intended command pipeline a string so it is never equal to string "0".

If you change it to the form you use later, it should work
Code:
if [ $( sudo -u virtualboxuser VBoxManage list runningvms | wc -l ) != "0" ]
Purists would say it is a numeric comparison so should be -eq 0, not = "0".

Purists would say that numeric tests should go in (( ... )) numeric tests not in [ ... ] tests in which case the comparison operator would have to be != or, more naturally, >.

More here.

Last edited by catkin; 11-10-2011 at 08:22 AM.
 
Old 11-10-2011, 08:21 AM   #3
el_pedro
LQ Newbie
 
Registered: Nov 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks v much catkin.
 
Old 11-10-2011, 08:24 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
AFAIK the VBoxManage list runningvms command only lists VMs being run by the user that issued the command, not all VMs on the local system.
 
Old 11-10-2011, 08:31 AM   #5
Juako
Member
 
Registered: Mar 2010
Posts: 202

Rep: Reputation: 84
If you are in Bash you can use this too:

Code:
echo "Virtual machine is $([[ ! $(VBoxManage list runningvms) ]] && echo "not ")running" >> $logfile

Last edited by Juako; 11-10-2011 at 08:35 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem running an Expect script within a Bash script mbeipi Programming 9 02-10-2018 05:00 AM
[SOLVED] How to make a bash script keep running in same terminal after it calls second script? JohnRock Linux - Newbie 4 06-25-2010 09:16 AM
Bash/perl script to determine network activity mrjoe42 Programming 1 07-07-2009 07:03 PM
How can I determine the path of a shell script that I'm running right now ? basher400 Linux - General 7 10-25-2006 08:31 AM
Does anyone know of a bash script that will determine if RPM exists on a system? jimwelc Linux - Software 3 12-28-2004 03:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Virtualization and Cloud

All times are GMT -5. The time now is 10:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration