Skip to content
View in the app

A better way to browse. Learn more.

Unraid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[6.12.13] Custom behavior when UPS is on battery power

Featured Replies

I just got my Cyberpower unit and enabled it in the Unraid settings. I set:

 

* Battery level to initiate shutdown (%): 40

* Runtime left to initiate shutdown (minutes): 20

 

My desired behavior when on battery power is to first stop the Plex container when the above threshold(s) is reached, wait 1 minute, then shut the system down. The reason why is because UptimeKuma monitors my Plex server every minute, and sends a Discord notification when the server status changes. I'd like to give UptimeKuma enough time to let my users to know that the server is down before the system actually shuts down. Is there a way to do this? Via custom scripts, or a plugin perhaps?

 

Edit: I found a partial solution to my query on the Unraid Support forum here. I've added the following command to the onbattery and offbattery scripts.

 

docker stop/start Plex-Media-Server

 

This partially solved my issue. I just need to find a way to trigger that line when a threshold is reached (either time remaining or battery percentage left, whichever occurs first). Instead of the above threshholds, it can be something like 45% for the battery level, and 25 minutes for the remaining runtime. That'll stop the container and the system will shut down once 40%/20min remaining is reached. If someone can help me with that.

 

Edit: So I managed to accomplish this after doing alot of research. Here are my steps for someone that comes across this post.

 

Note: These instructions will be for the "Network UPS Tools (NUT) for UNRAID" plugin. The built-in UPS tool in Unraid (apcupsd) doesn't turn off my Cyberpower UPS once the system shuts down. That's why I had to go with NUT. I need the "turn off UPS when system shuts down" feature because I want the system to automatically start when power is back (You'll have to enable that in the BIOS). You'll also see difference battery and time remaining values than my initial post, I just prefer it this way but feel free to use your own values.

 

  1. If enabled, disable the APC UPS Daemon that's built in Unraid via Settings -> UPS Settings -> Start APC UPS daemon -> select No and apply.
  2. Install the "Network UPS Tools (NUT) for UNRAID" plugin.
  3. Navigate to Settings -> NUT Settings
    1. "Start Network UPS Tools Service" -> Yes
    2. "NUT Mode" -> Standalone.
    3. I entered a username and a password although I never used it and don't think it's required.
    4. "UPS Name" -> I named mine cyberpowerups (note this, as it will be needed later on)
    5. For "UPS Driver" click on the magnifier and use the driver indicated in the popup for your UPS model.
    6. "UPS Battery Runtime Attribute" -> battery.runtime
    7. "Shutdown Mode" -> I set mine to Battery Level to go with battery percentage.
    8. "Battery level to initiate shutdown" -> 60 (at your discretion)
    9. "Disable UPS after shutdown" -> Yes (at your discretion. This cut power to your system once the server is powered off. To turn the system back on automatically when power is back, you need to enable auto boot in your BIOS, check your motherboard manual on how to do that).
    10. "Battery replacement Notification" -> Yes (why not?).
    11. Click on Apply, this will enable your UPS via NUT.
  4. Install the "User Scripts" plugin. This will be used to create scripts that monitor the UPS and perform actions as necessary (stop the Plex container in my case).
  5. Navigate to Settings -> User Scripts
  6. Click on "Add New Script" (we will add 2 new scripts)
  7. I named mine check_ups_and_stop_plex 
  8. Click on the gear icon to edit the script and copy the code block below tagged "check_ups_and_stop_plex" (customize to your needs). This script will monitor and stop Plex if 65% of battery level is reached or 1 hour of runtime remaining. Make sure to replace "cyberpowerups" by the UPS name you have entered in the NUT settings. Save changes.
  9. In the event that power returns between stopping the Plex container and the system shutting down, we need an additional script to start the Plex server back automatically. Follow the same steps to add a new script and copy the code block below tagged "start_plex_on_power_return" and customize the script to your needs. This script will start the Plex containing once power has returned and there is sufficient charge or remaining runtime in the UPS (65% or 1 hour in my case). Make sure to replace "cyberpowerups" by the UPS name you have entered in the NUT settings. Save changes.
  10. For both scripts, I chose to monitor every minute. Select Custom from the dropdown menu for each script and enter * * * * *. Look into Cron to customize to your needs but * * * * * == execute the script every minute.

 

#!/bin/bash

# check_ups_and_stop_plex
#!/bin/bash

# Check if Plex container is running
plex_status=$(docker inspect -f '{{.State.Running}}' Plex-Media-Server)

# Only proceed if Plex is running
if [[ "$plex_status" == "true" ]]; then
    # Get UPS status from NUT
    ups_status=$(upsc cyberpowerups ups.status)

    # Check if UPS is on battery power (OB means "On Battery")
    if [[ "$ups_status" =~ "OB" ]]; then
	
		# Get UPS data from NUT
	    battery_charge=$(upsc cyberpowerups battery.charge)
		runtime=$(upsc cyberpowerups battery.runtime)
	
        # Stop Plex if battery charge <= 65% or runtime <= 3600 seconds
        if [[ "$battery_charge" -le 65 || "$runtime" -le 3600 ]]; then
            docker stop Plex-Media-Server
            
            # Ensure the config directory exists
            mkdir -p /mnt/cache/config
            
            # Write the reason for stopping to a file
            echo "UPS" > /mnt/cache/config/plex-stopped-by
        fi
    fi
fi

 

--------------------------------------

 

#!/bin/bash

# start_plex_on_power_return
#!/bin/bash

# Check if Plex container is stopped
plex_status=$(docker inspect -f '{{.State.Running}}' Plex-Media-Server)

# Only proceed if Plex is stopped
if [[ "$plex_status" == "false" ]]; then

    # Check if the Plex container was stopped due to a UPS event
    if [ -f /mnt/cache/config/plex-stopped-by ] && grep -q "UPS" /mnt/cache/config/plex-stopped-by; then
	
        # Get UPS status from NUT
        ups_status=$(upsc cyberpowerups ups.status)

        # Check if UPS is on line power (OL means "On Line")
        if [[ "$ups_status" =~ "OL" ]]; then
		
			# Get UPS data from NUT
		    battery_charge=$(upsc cyberpowerups battery.charge)
			runtime=$(upsc cyberpowerups battery.runtime)
		
            # Start Plex if battery charge > 65% and runtime > 3600 seconds
            if [[ "$battery_charge" -gt 65 && "$runtime" -gt 3600 ]]; then
                docker start Plex-Media-Server
                # Clear the stop reason file after starting
                rm /mnt/cache/config/plex-stopped-by
            fi
        fi
    fi
fi

 

Edit: After thinking about it for some time, I realized that there is a problem. When stopping the container manually for whatever reason (say for updating the container or configurations or any X reason) and you don't want the script to start it back, I had to add a few lines in both the stop and start scripts. The stop script will create and write to a file stating the reason why the container is stopped ("UPS" is written to the file). The start script will read that file to see if it was stopped manually or automatically by the stop script. It'll use this information to determine if it should start the container again.

Edited by h4g0p
Updated start and stop scripts. Explanation provided at the end of the post.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.