How I got my Plex Linux Computer working with suspend/sleep after inactivity, and Wake On LAN

Server Version#: 1.13.4.5271-200287a06
Player Version#: ??

I have my desktop at home basically being my Plex server, and I want to save on electricity (it’s a big electricity sink!). While Plex is streaming, I want to keep it awake, but after N minutes (I did 30 minutes using the OS mechanisms itself), I want it to go to sleep. Then I want to use a simple Wake On LAN button on my phone to wake the computer up, and relatively quickly get Plex streaming again. I’m mostly streaming to my Roku device/TVs.

The quick high level is that I grabbed (from a few places around these forums and the internet) some scripts, tweaked and added to them, and you should have the final product here.
What I needed was:

  1. A script at /etc/pm/sleep.d/00-plexresume (conditionalized with your linux OS username that the X display server is installed as, you’ll need to be running a display server for the method I’m using)
  2. A script at /etc/pm/sleep.d/01-plexkeepalive (conditionalized with your “X-Plex-Token” auth token for API calls, more on how to find this later)
  3. xdotool a utility to automate X events, such as mouse and keyboard events. This is used to force the OS to think that the computer is still “active” and therefore won’t be suspended. This can be found in most package managers easily enough, typically with the package name of simply “xdotool”.
  4. Setup suspend after N minutes of activity through the OS itself (most window managers have this setting available through their settings UI)
  5. A “wake on LAN” app. I’ll point you to the Android app I used which has a 1 tap widget you can put on your homescreen

Optionally you might need these extra steps:

  1. An after-suspsend-resumed /etc/pm/sleep.d/05-network-restart to get networking to work after you resume. After some recent OS updates, my network stack wouldn’t work at all until I restarted the network-manager service. This just automates doing that on resume. You may not need this.
  2. An ability to change the OS’s “# of minutes inactive to suspend” down to 1 minute, so that you can quickly test these scripts to ensure they are working to your liking. If you’re using XFCE as your window manager, you’re in luck, because I have the way to bring it down to 1 minute instead of the 10 minutes min the slider lets you set in the UI.

Very quickly, the method here is that we let the OS start to suspend the machine after N minutes of inactivity, but we make a script that runs first, looks to see if the plex server is streaming anything currently, and if so, halts the suspend. Then the OS runs the resume scripts of higher priority which includes our resume script. The resume script runs (which incidentally also runs when you actually resume from sleep mode) that uses “xdotool” to move the mouse by a few pixels and convince the computer that user activity has occurred, pushing back the amount of time until the computer next checks to see if it should sleep for another N minutes (whatever your OS sleep/suspend timeout is). Finally all the suspend/resume events get logged to /tmp/plex_sleep_log, which should help you debug if it doesn’t work right away for you.

Here’s the resume script you should place at /etc/pm/sleep.d/00-plexresume (as root).
Take special note of the “X_DISPLAY_USERNAME” line. You’ll have to set this to whaterver user is running the X server. This is because the suspend/resume scripts the OS uses all get invoked as root.
Hi, I’m Scott :slight_smile: Also I’ve assumed that the display is running on the default “0” port (DISPLAY=:0). Change this as well if this is different for you.

#!/bin/bash

X_DISPLAY_USERNAME=scott

case "$1" in
    resume)
        echo "[$(date +"%Y.%m.%d-%T")] Resuming!!..." >> /tmp/plex_sleep_log
        su ${X_DISPLAY_USERNAME} -c "DISPLAY=:0 /usr/bin/xdotool getmouselocation | grep 'x:1 y:1 '" > /dev/null
        if [ "$?" == "0" ]; then
            su ${X_DISPLAY_USERNAME} -c "DISPLAY=:0 /usr/bin/xdotool mousemove 9 9"
        else
            su ${X_DISPLAY_USERNAME} -c "DISPLAY=:0 /usr/bin/xdotool mousemove 1 1"
        fi
        exit 0
        ;;
esac

Here is the suspend script you should place at /etc/pm/sleep.d/01-plexkeepalive (as root).
For this script, take note of the X_PLEX_TOKEN at the top, which you should replace with your Plex API token. I’ll describe below how to find that.
Also I assume that your plex server is running at localhost and port 32400, but if not, change that in the script below as well.

#!/bin/bash

X_PLEX_TOKEN=YOUR_PLEX_TOKEN_HERE

case "$1" in
    suspend)
        number_of_sessions=$(curl -s localhost:32400/status/sessions?X-Plex-Token=${X_PLEX_TOKEN} | sed -n "s/.*MediaContainer size=\"\(.*\)\".*/\1/p")
        if [ ${number_of_sessions} -gt 0 ]; then
            echo "[$(date +"%Y.%m.%d-%T")] Number of streamers = ${number_of_sessions} Plex session active, cancel suspend" >> /tmp/plex_sleep_log
            exit 1
        else
            echo "[$(date +"%Y.%m.%d-%T")] Number of streamers = ${number_of_sessions} Plex session -IN-active, going to sleep now   zzzzzzzz........" >> /tmp/plex_sleep_log
            exit 0
        fi
        ;;
esac

There are instructions on how to find your X-Plex-Token here, but they don’t match up to the UI on my plex web app, so what I had to do was login to plex, browse to a library item on the server, in the options ("…") menu for that item, select “Get Info”. This will bring up a dialog, which at the bottom of there will be a “View XML” link. Click that, and in the URL it takes you to, at the end of the URL should be your “X-Plex-Token”.

In case you have the same issue I did with networking not working properly after system resume, here’s a script to fix that at /etc/pm/sleep.d/05-network-restart

#!/bin/bash

case "$1" in
    resume)
        service network-manager restart
        exit 0
        ;;
esac

If you are using XFCE for your windowing manager, and you need a way to set your inactivity timeout to 1 minute (instead of the normal min of 10 in the UI), open the Settings Editor (Settings → Settings Editor from the main OS menu). In there select “xfce4-power-manager” from the left, then on the right, browse to “xfce4-power-manager → inactivity-on-ac”, and set that to 1. That’s an “Integer” type key, and it might not be present. If not just create it. The change should be immediate (no need to restart or reload UI).

Finally get yourself a “wake on LAN” app for your phones. There are many of them, and I tried about 5 different ones, looking for one with a good small widget. At the end of the day, some of them just didn’t work, but about half of the apps did, and I settled for one for Android named “Wake On LAN”, with a blue background image for the app, and from the developer “Sigma-Square UG”. Either way, to make these apps work you need the MAC address (“hardware” address) of your network card, and some of them take/require an IP address as well, but it shouldn’t be necessary. To get this information, at a terminal run ifconfig -a. There might be quite a few network adapters, but if you’re using ethernet it’s probably “eth0” or “eth1”. If your plex server is a laptop the name of the adapter might be more creative. Look for the “HWaddr 09:fe:7d:ba:82:9a” type of entry. That will be your MAC address. The “inet addr:192.168.1.101” line will be your IP address.

I hope this helps someone else! I know it would have saved me some time…

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