Skip to content

Instantly share code, notes, and snippets.

@e13h
Created August 25, 2020 23:24
Show Gist options
  • Save e13h/8895715070612b559f830e470680b9ec to your computer and use it in GitHub Desktop.
Save e13h/8895715070612b559f830e470680b9ec to your computer and use it in GitHub Desktop.
Setting up your Linux computer to not sleep while Plex Media Server is streaming

Prevent Computer From Sleeping While Plex Media Server is Streaming

With the help of Google, I was able to piece together a solution for preventing my Linux computer from sleeping while streaming something via Plex.

My workflow right now is to use an app called Wake Me Up to send a wake-on-lan signal to my Linux computer running Plex Media Server, which allows my Plex client apps to discover the server. The server won't go to sleep while there are any streams running from the server as long as the service defined below is running.

References

Instructions for enabling keep-awake service

Note in order for the service to run, you need to make sure that curl is installed, and that you have set the environment variable X_PLEX_TOKEN (see references section above for how to find your token).

One way to do this is by running the following command in your terminal (assuming you use bash)

echo "export X_PLEX_TOKEN=PasteYourTokenHere!" >> ~/.bashrc
source ~/.bashrc

Save the file plexkeepawake.service inside the directory /etc/systemd/system/ (you'll need to use sudo).

Start the service. Note that by using the keyword enable, the service will start now and any time the computer reboots.

systemctl enable plexkeepawake.service
[Unit]
Description=Inhibit suspend if Plex is streaming media
Before=sleep.target
[Service]
Type=oneshot
ExecStart=/bin/sh -c "if [ `curl localhost:32400/status/sessions?X-Plex-Token=$X_PLEX_TOKEN 2>/dev/null | sed -n 's/.*MediaContainer size=\"\(.*\)\".*/\1/p'` -gt 0 ]; then exit 1; else exit 0; fi"
[Install]
RequiredBy=sleep.target
@maurogsp
Copy link

maurogsp commented Aug 2, 2021

Hi Evan, how are you? I've managed to follow your instructions and got my server to prevent from sleeping while plex is streaming, but I realized that once it prevents from sleeping, then it never goes to sleep again, even if plex is no longer streaming. Do you know what this behavior could be due to?
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment