Results 1 to 7 of 7

Thread: Scripting problem: UPS to shutdown VirtualBox VM

  1. #1
    Join Date
    Jul 2012
    Beans
    7

    Scripting problem: UPS to shutdown VirtualBox VM

    I am running 12.04 and recently added a CyberPower UPS to my system. At first I ran into a problem where the system was incorrectly saying the battery was critical when I plugged in the UPS. I was able to work around this with dconf-editor by setting the battery critical action to nothing.

    Then I installed CyberPowers Powerpanel for Linux. I have that working and it correctly reports on the status of my UPS. Powerpanel will invoke a script when AC is disconnected as well as when the battery reaches a certain percentage.

    The line in the script (/etc/pwrstatd-powerfail.sh) is:

    gksu -u shawn -l -d -w vboxmanage controlvm "Windows XP" savestate

    What I am stuck on is if I am in /etc and use
    sudo ./pwrstatd-powerfail.sh

    the script runs properly and saves the state of the virtual machine.

    However, when the script is invoked by the pwrstatd daemon the virtual machine is not being saved. I know the script itself is being run as it has an echo command that pops up a message in terminal.

    What am I doing wrong?

    Thank You,

    Shawn

  2. #2
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    6,759
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Scripting problem: UPS to shutdown VirtualBox VM

    Hi Shawn67.

    I'm not very familiar with Powerpanel, but I understand there's a log you can check:
    Code:
    /var/log/pwrstatd.log
    Could you post the complete content of the file pwrstatd-powerfail.sh?

    Regards.

  3. #3
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Scripting problem: UPS to shutdown VirtualBox VM

    Quote Originally Posted by Shawn67 View Post
    gksu -u shawn -l -d -w vboxmanage controlvm "Windows XP" savestate
    Use sudo not gksu. Also the true test would be to see if it is being run by root or as a certain user, who might not have sudo rights.

    *digs thru shutdown script*

    Code:
    VBOXUSER=shawn
    SU="sudo -H -u $VBOXUSER"
    VBOXMANAGE="/usr/bin/VBoxManage --nologo"
    $SU $VBOXMANAGE controlvm "Windows XP" savestate
    If you don't want to do it that way, you can combine it into one line:

    Code:
    sudo -H -u shawn /usr/bin/VBoxManage --nologo controlvm "Windows XP" savestate
    You can find my startup and shutdown script for virtualbox here:
    http://charlesa.net/scripts/linux/vi...ipt-ubuntu.php
    Last edited by CharlesA; April 16th, 2013 at 05:47 AM. Reason: updated url
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

  4. #4
    Join Date
    Jul 2012
    Beans
    7

    Re: Scripting problem: UPS to shutdown VirtualBox VM

    Sure, the script is:

    Code:
    #!/bin/sh
    echo "Warning: Utility power failure has occurred for a few seconds, system will be shutdown soon!" | wall
    
    export RECEIPT_NAME
    export RECEIPT_ADDRESS
    export SENDER_ADDRESS
    gksu -u shawn -l -d -w vboxmanage controlvm "Windows XP" savestate
    
    #vboxmanage controlvm "Windows XP" savestate
    #
    # If you want to receive event notification by e-mail, you must change 'ENABLE_EMAIL' item to 'yes'.
    # Note: After change 'ENABLE_EMAIL' item, you must asign 'RECEIPT_NAME', 'RECEIPT_ADDRESS', and 
    # 'SENDER_ADDRESS' three items as below for the correct information.
    #
    
    # Enable to send e-mail
    ENABLE_EMAIL=no
    
    # Change your name at this itme.
    RECEIPT_NAME="user name"
    
    # Change mail receiver address at this itme.
    RECEIPT_ADDRESS=user_name@company.com
    
    # Change mail sender address at this itme.
    SENDER_ADDRESS=user_name@company.com
    
    # Execute the 'pwrstatd-email.sh' shell script
    if [ $ENABLE_EMAIL = yes ]; then
       /etc/pwrstatd-email.sh
    fi
    Thanks,

    Shawn

  5. #5
    Join Date
    Jul 2012
    Beans
    7

    Re: Scripting problem: UPS to shutdown VirtualBox VM

    Charles,

    Thank you! That seemed to do the trick.

    Shawn

  6. #6
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Scripting problem: UPS to shutdown VirtualBox VM

    Outstanding. Don't forget to mark the thread as solved from thread tools at the top.
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

  7. #7
    Join Date
    Jul 2012
    Beans
    7

    Re: Scripting problem: UPS to shutdown VirtualBox VM

    Thanks again for the help.

    I just changed powerpanel to not run the script on loss of AC and instead run the script when battery capacity hits 35%. Now when I hit 35% the script runs and saves the VM state, then it waits about 25 seconds and shuts down Ubuntu. 2 minutes after the shutdown is issued the UPS turns its outlets off.

    When AC is restored the UPS waits 10 seconds and then turns on the AC outlets. The bios in the computer is set to Always turn on when AC is restored so this turns back on the server. So now the server (and the VMs) are automatically shutting down and turning back on when power changes. Perfect!

    Thanks,

    Shawn

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •