Success Avoiding Sleep While Plex Server is Streaming on Arch KDE-Plasma

I finally have success preventing my computer from entering sleep/suspend while the Plex Media Server is streaming on Arch with KDE-Plasma.

I know absolutely nothing about shell scripts.

Within Plasma Energy Saving there is an option to run a script. I point it to a simple script to check if the port is open.

I borrowed the command heavily from collindelker.com/wp/2015/07/plex-linux-prevent-sleep/http://

Don’t forget to make the script executable.

Here it is:

          s=$(curl localhost:32400/status/sessions | sed -n "s/.*MediaContainer size=\"\(.*\)\".*/\1/p")
          if [ $s -gt 0 ]
          then
              echo "[$(date +"%Y.%m.%d-%T")] Number of streamers = $s Plex session active, cancel suspend" >> /home/[user]/plex_sleep_log
                exit 0
         fi
    
        if [ $s -eq 0 ]
        then
            echo "[$(date +"%Y.%m.%d-%T")] Number of streamers = $s Plex session -IN-active, going to sleep now   zzzzzzzz........" >> /home/[user]/plex_sleep_log
            qdbus org.kde.Solid.PowerManagement /org/freedesktop/PowerManagement Suspend
            exit 0
        fi

That’s it.
I’m sure there are more elegant solutions.
I’ve only tried this a couple of times so we’ll see how it works in the wild.

-i_o_i

This method no longer works, the server never sleeps because it throws an error when executing this script.
Collin Delker proposed a solution at collindelker.com/2016/10/16/plex-linux-prevent-sleep-take2.html

I haven’t been able to make it work as of yet.

I’m using this command in the comments with a slight modification for ss -p in lieu of netstat -p and a ; was omitted before else.

ExecStart=/bin/sh -c "if [ 'ss -p | egrep -c STREAM.*CONNECTED.*Plex' -gt 0 ]; then exit 1; else exit 0; fi"

Also tried ss -p | grep "ESTAB" | grep -c "plex"', but still doesn’t prevent sleep.

systemctl status reports inactive (dead)

As hoffmajf noted, the original script doesn’t work anymore (because it requires a “X-Plex-Token” API token to call). I’ve put a full walkthrough of how I’ve gotten this working over here:
https://forums.plex.tv/t/how-i-got-my-plex-linux-computer-working-with-suspend-sleep-after-inactivity-and-wake-on-lan/341703
Hope this helps!