I have a script which checks to see if there are any virtual machines running and, if there are, gracefully shuts them down (uses VBoxManage), the idea being that if I get called away from my machine then databases running in VMs will not suffer problems from time jumps when they are resumed.

The script runs properly from the command line but not when called from the suspend-to-hibernate.service file I've created!

What should happen is that the script calls VBoxManage to list running machines, pipes that into awk to extract the name of the VM, dumping it all into another file which is then called to close down the machines.

It runs well until the call to VBoxManage - it appears that the command simply doesn't run. The /usr/bin/VBoxManage is actually a link to a script that calls /usr/lib/virtualbox/VBoxManage. I've tried calling this direct but still not running.

The stop-vms.log shows that the 'on the fly' script does actually run.

It's been a while since I've done any unix scripting & I'm feeling like I've missed something very simple...


Description=Script to check for VirtualBox VMs and pause them
Conflicts=hibernate.target hybrid-suspend.target
Before=suspend.target sleep.target

ExecStart=/bin/sh /home/steve/Documents/scripts/stop-vms.sh suspend


# some vars:


# check if script file exists, if not create & make executable
if [ ! -e $NEWSCRIPT ]
    touch $NEWSCRIPT
    chmod 775 $NEWSCRIPT

# tell the script where the shell is
echo '#!'$SHELL > $NEWSCRIPT
echo '#starting at:' `date` >> $NEWSCRIPT

# get list of running machines and dump into script file

echo 'echo "running vms" >> /home/steve/logs/stop-vms.log' >> $NEWSCRIPT      

/usr/bin/VBoxManage list runningvms | awk  '{print "/usr/lib/virtualbox/VBoxManage controlvm " substr($line, 1, index($line,"{")-2) " acpipowerbutton >> /home/steve/logs/stop-vms.log "} '  >> $NEWSCRIPT

echo 'vms done, about to run script  at ' `date` >> $LOGFILE

# run the script

# now wait to make sure the previous command has had time to finish...

sleep 30

# report on last run...
echo >> $NEWSCRIPT
echo '# Last Run: ' `date` >> $NEWSCRIPT

Expected stopvms script (when run from terminal)

#starting at: Wed 31 Jan 10:24:34 GMT 2018
echo "running vms" >> /home/steve/logs/stop-vms.log
/usr/lib/virtualbox/VBoxManage controlvm "Dev Mysql GlassFish" acpipowerbutton >> /home/steve/logs/stop-vms.log 

# Last Run:  Wed 31 Jan 10:25:04 GMT 2018

Actual when run from suspend-to-hibernate.service:

#starting at: Wed 31 Jan 10:59:26 GMT 2018
echo "running vms" >> /home/steve/logs/stop-vms.log

# Last Run:  Wed 31 Jan 10:59:56 GMT 2018
  • Are Dev, Mysql and Glassfish 3 separate VMs? Is the stopvms script created as you'd expect? – Arronical Jan 31 '18 at 11:55
  • @Arronical "Dev Mysql GlassFish" is the name of the single VM that was running at the time. It is enclosed in quotes for the benefit of VBoxManage and, given that the script runs form the command line then it is as I would expect it to be when it is run from the command line but not from the suspend service. If there were more VMs running there would be a single line for each. – Hippy Steve Jan 31 '18 at 13:42

Your Answer

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

Browse other questions tagged or ask your own question.