UPS + MBP - Setting MBP to automatically force quit Apps & shut down at a certain battery percentage

Discussion in 'Mac Basics and Help' started by ZapNZs, Oct 11, 2017.

  1. ZapNZs macrumors 68020

    ZapNZs

    Joined:
    Jan 23, 2017
    #1
    I would like to have a means of setting my MacBook Pro (2014 15in, El Cap 10.11.6) to shut down at something like 95% capacity remaining when running on battery power, and that this shutdown will override any save-change dialogs and, if necessary, force quit all open Apps to ensure a successful shutdown (even if that means losing any changes to unsaved documents). Preferably, there would be an easy means of toggling this setting on/off so when I intentionally use the system on battery power it is easy to disable this.

    (side note - why would I want to do this? - My MBP is connected to a Thunderbolt hub and about 7+ external drives at any given time, some of which are SSDs that lack true PLP. All of these drives and the hub are now connected to a UPS [APC BR1000G, if it matters]. The Maker of this UPS does not have [current] Mac-compatible software. If the power goes out while I am away or asleep, the UPS will run out of battery power long before the MBP will, and this will result in all of the external drives being improperly ejected - which is exactly what I want to avoid. If I can set the MBP to shut down at 95% capacity, then the MBP will shut down before the UPS runs out of power, safely ejecting the drives, and when the UPS runs out of capacity then the drives are being shut down only after they have been safely ejected, potentially avoiding issues induced by unexpected power loss, especially in regards to SSDs without tPLP. With the changes to power management features in macOS, the UPS option to shutdown is no longer available - it only shows sleep options now, and even when this feature was accessible it did not have an option to force quit all Apps. The apcupsd daemon also doesn't quite meet my needs, hence my seeking of this very specific solution. [I imagine I am not the only one who is interested in a solution like this...???])


    What is the best means of accomplishing this?
     
  2. Weaselboy Moderator

    Weaselboy

    Staff Member

    Joined:
    Jan 23, 2005
    Location:
    California
    #2
    https://www.controlplaneapp.com/feature-list/

    There is a nice app called Control Plane that might help with this. Essentially the app watches for whatever changes you tell it to, like connecting a new monitor or change in power supply, then based on that change can execute specific actions. You can also setup custom actions.

    I'm not clear from your post though... is the MBP connected to the UPS? If not, I'm thinking control plane would notice there is no more AC power, then run an action we setup to do what you want.

    Maybe install the app and see if it will detect loss of AC power, then we can work on what is next.

    I see that app allows ejecting drives as one of the actions, so that is half the battle.
     
  3. ZapNZs thread starter macrumors 68020

    ZapNZs

    Joined:
    Jan 23, 2017
    #3
    Thank you so much!

    Currently, the MBP is not connected to the battery portion of the UPS. It is connected to the surge/voltage regulation side, so it will not receive power from the UPS should the power go out. If this matters, the estimated run time based on the load currently placed on the UPS when on battery power is a little over 200 minutes - and based on testing (using less important drives to simulate the same load), this estimate seems to be very accurate.

    So, I downloaded the App, gave it the security permissions/evidence sources permissions needed to make changes to the computer, and wrote a test rule where:

    IF the system goes to battery power (type: power/ description: battery/ context: automatic-named Auto Shutdown on UPS power/ confidence: 100%)
    THEN, open Calculator App (type: open/ when: on arrival only/ context: same as above /delay: 30 seconds)

    This works correctly, detecting when AC power has been lost and then it opens Calculator. Interestingly, if I connect the UPS' USB-to-ethernet cable to the MBP, then the rule will only function when BOTH the USB to the UPS AND and power adapter are disconnected (presumably because this cable connection allows macOS to know the status of the UPS, as evidenced by the power status in iStat Menus and via the System Profiler). So macOS knows what the UPS' battery level is - but given it is not on the UPS outlets, the 1-minute sleep rule shown in the Profiler would presumably not apply.

    However, with the 30 second delay, if I remove both the power cord and the USB-->ethernet cord plugged into the UPS, this initiates a 30 second countdown - but, if before the 30 second delay expires I plug both back in, the rule still executes (i.e., it does not cancel the command if the conditions are changed.)

    The reason I was originally thinking about having the shutdown command at 95% was to account for extremely short outages where the power goes out for a minute or two and comes back on (even though this unit is a modified sine wave, given the HDDs and SSDs all operate on DC power as does the Thunderbolt 2 hub, I figure this should not be an issue having them operate on a simulated sine wave UPS for just a few minutes.) Would it be better to think of the MBP and UPS from a separate line of reasoning? For example...
    UPS Power Plan.jpg
     
  4. Weaselboy Moderator

    Weaselboy

    Staff Member

    Joined:
    Jan 23, 2005
    Location:
    California
    #4
    Oh good... so Control Plane works as the trigger for this.

    Honestly, some of the if/then/else scripting required here is over my head. I am just a plebe at AppleScript.

    I did piece this together and tested, and it works fine. It will eject all mounted disks, wait 60 seconds, then force a shutdown overriding app save dialogues. I'm sure there is much more that could be done with this, but having Control Plane launch this AppleScript would get you most of what you are after I think.

    Code:
    tell application "Finder"
        eject the disks
    end tell
    
    delay 60
    
    tell application "System Events"
        ignoring application responses
            shut down
        end ignoring
    end tell
     
  5. ZapNZs thread starter macrumors 68020

    ZapNZs

    Joined:
    Jan 23, 2017
    #5
    Weaselboy I <3 you. It works great! Thank you so much!

    One last question - so if I modify it to add a 5-minute delay to the ejecting of the drives, and a 7 minute delay to the shutdown, this would add a delay in the shutdown sequence for if the power outage was only a short flicker or only lasted like a minute or two.

    How can I cancel the script if the power comes back on while the script is running during the delay? Can a master script be written that recalls the one below? (which I would initiate if the power turns back on, also using Control Plane as the trigger)


    Code:
    tell application "Finder"
    delay 300
    
        eject the disks
    end tell
    
    delay 420
    
    tell application "System Events"
        ignoring application responses
            shut down
        end ignoring
    end tell
     
  6. Weaselboy Moderator

    Weaselboy

    Staff Member

    Joined:
    Jan 23, 2005
    Location:
    California
    #6
    I'm thinking if you save the first AppleScript as an app, let's say called ShutDown.... then make this AppleScript run off ControlPlane if it senses power, that would quite the ShutDown app (script).

    I'm sure others know a more elegant way, but this should work.

    Code:
    tell application "ShutDown" to quit
     

Share This Page

5 October 11, 2017