Bash Script to Warn/Alert Users of Imminent Shutdown?

So, here in Los Angeles, we’ve been having a gaggle of rolling blackouts. Thankfully, I have a set of UPS’s which prevent any damage being done. However, there’s one thing I’ve been having trouble finding on the forums.

Are there any bash scripts that can be run, in terminal (Mac), that can send a message to users currently connected to my Plex server?

My Cyperpower UPS can run a bash script prior to shutdown. Which is great, because I can receive email alerts from my system, if there’s a power outage. But I’d like to go a step further.

What I’d like to blast out, is an annoying message stating, “Due to a power outage, this PLEX Media Server will shut down in 30 sec. Apologies, for any inconvenience.”

Any help would be greatly appreciated.

Untested, but try this:

Using like curl, issue a GET towards your PMS like:

<IP_OF_PMS>:32400/status/sessions?X-Plex-Client-Identifier=Plex_Forums&X-Plex-Token=<YOUR_TOKEN>

Above will return active sessions, so grap the Session Id, that would look like:

Session id="2W6y3xeptpdoYqBxeEvA"

Then issue a new Get like:

<IP_OF_PMS>:32400/status/sessions/terminate?sessionId=token%3D<SESSION_TOKEN_FROM_ABOVE>&reason=<ANY_URL_ ENCODED_TEXT_ TO_SEND>&X-Plex-Client-Identifier=Plex_Forums&X-Plex-Token=<YOUR_TOKEN>

Sample url to send, and nope, not my real access token :wink:
https://PMS_IP:32400/status/sessions/terminate?sessionId=2W6y3xeptpdoYqBxeEvA&reason=Lost+Power&X-Plex-Client-Identifier=3oqnywstl4gyupfnsd8vrh98&X-Plex-Token=MY_PLEX_TOKEN

To find your token, look here: Finding an authentication token / X-Plex-Token | Plex Support

And above will immediately stop playback and show Lost Power on the player

1 Like

Thank you.

I’m hitting a few roadblocks. Easily found the Plex Token, but nothing I try can give a session ID. Do I have to get a new token and/or session ID for every stream/streamer? I’m pretty sure I can create a script to do that. I’m just unsure how to get the session IDs, properly. Running a version of your command just gives an output of:

[1] 49383
-bash: X-Plex-Token=xxxPLEXxxTOKENxxx: command not found

PLEX-Mac-mini:local admin$ <html><head><title>Unauthorized</title></head><body><h1>401 Unauthorized</h1></body></html>
[1]+  Done                    curl -X GET 127.0.0.1:32400/status/sessions?X-Plex-Client-Identifier=alexvanhaaff

I’m also guessing that your Plex-Client-Indentifier would be the individual User Accounts, or would it be the PlexPass account name?

Is this something that might require another sort of authentication? Like a PIN?

In above, you did NOT use your token, which is what authenticates you!

Should be like:

curl -X GET 127.0.0.1:32400/status/sessions?X-Plex-Client-Identifier=alexvanhaaff&X-Plex-Token=<INSERT_TOKEN_HERE>

And 4 the fun of it, I created a bash script
You’ll need to customize first 4 lines

#!/bin/sh

PMS_IP='192.168.1.14'
TOKEN='YOUR_TOKEN'
MSG='Power+off+now'
CLIENT_IDENT='SomeRandomStringWithoutSpace'


#Start by getting the active sessions

sessionURL="http://$PMS_IP:32400/status/sessions?X-Plex-Client-Identifier=$CLIENT_IDENT&X-Plex-Token=$TOKEN"
response=$(curl -i -k -L -s $sessionURL)
sessions=$(printf %s "$response"| grep '^<Session*'| awk -F= '$1=="id"{print $2}' RS=' '| cut -d '"' -f 2)

# Active sessions id's now stored in sessions variable, so convert to an array
set -f                      # avoid globbing (expansion of *).
array=(${sessions//:/ })
for i in "${!array[@]}"
do
    echo "Need to kill session: ${array[i]}"
    killURL="http://$PMS_IP:32400/status/sessions/terminate?sessionId=${array[i]}&reason=$MSG&X-Plex-Client-Identifier=$CLIENT_IDENT&X-Plex-Token=$TOKEN"
    # Kill it
    response=$(curl -i -k -L -s $killURL)
    # Get response
    http_status=$(echo "$response" | grep HTTP |  awk '{print $2}')
    if [ $http_status -eq "200" ]
    then
      echo "Succes with killing of stream ${array[i]}"
    else
      echo "Something went wrong here"
    fi
done
3 Likes

I think he tried to use his token, but the unescaped & got eaten and interpreted by the shell. The bash output [1] 49383 means a job got spawned into the background, and the -bash : X-Plex-... detritus means bash tried to execute the rest of the URI as a command.

I think wrapping the argument in some nice protective quotes will do it:

curl -X GET “127.0.0.1:32400/status/sessions?X-Plex-Client-Identifier=alexvanhaaff&X-Plex-Token=<INSERT_TOKEN_HERE>”

@dane22 your script is spiffy.

2 Likes

That was it exactly it. Thank you.

Hot diggity damn! That works a treat! Confirmed from two different households, four different systems and three different users. Thank you very much!

1 Like

You’re welcome

Thanks :wink:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.