Mike Slinn

Optimizing Plex Media Server on Ubuntu

Published 2022-07-23.
Time to read: 2 minutes.

This page is part of the posts collection, categorized under Media.

Plex Media Server is a free, full-featured media manager that is available for Linux, Windows, Mac, and is built into some NAS devices. I run it along with other processes on an Ubuntu server in my apartment.

We can learn a lot about the Plex Media Server processes using the information provided by systemctl status:

Shell
$ sudo systemctl status plexmediaserver
● plexmediaserver.service - Plex Media Server
     Loaded: loaded (/lib/systemd/system/plexmediaserver.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-07-22 12:13:32 EDT; 23h ago
    Process: 2307 ExecStartPre=/bin/sh -c /usr/bin/test -d "${PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR}" || /bin/mkdir -p "${PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR}" (code=exited, status=0/SUCCESS)
   Main PID: 2329 (Plex Media Serv)
      Tasks: 45 (limit: 38369)
     Memory: 25.2G
        CPU: 42min 54.673s
     CGroup: /system.slice/plexmediaserver.service
             ├─2329 "/usr/lib/plexmediaserver/Plex Media Server"
             ├─3404 "Plex Plug-in [com.plexapp.system]" /usr/lib/plexmediaserver/Resources/Plug-ins-6b0e31a64/Framework.bundle/Contents/Resources/Versions/2/Python/bootstrap.py --server-version 1.27.1.5916-6b0e31a64 /usr/lib/plexmediaserver/Resources/Plug-ins-6b0e31a64/System.bundle
             └─3521 "/usr/lib/plexmediaserver/Plex Tuner Service" /usr/lib/plexmediaserver/Resources/Tuner/Private /usr/lib/plexmediaserver/Resources/Tuner/Shared 1.27.1.5916-6b0e31a64 32600 

The above tells us that Plex Media Server runs under a cgroup, which is “a Linux kernel feature which allow processes to be organized into hierarchical groups whose usage of various types of resources can then be limited and monitored”. The cgroup is defined in /lib/systemd/system/plexmediaserver.service. Here is mine:

/lib/systemd/system/plexmediaserver.service
# DO NOT EDIT THIS FILE DIRECTLY!
#
# Plex Media Server's variables can be customized by creating an 'overide.conf'
# file using 'systemctl edit plexmediaserver' which will create the following;
# /etc/systemd/system/plexmediaserver.service.d/override.conf
#
# An example of the override.conf would be as follows if you wished to edit
# your user, group, temp directory, or app support directory (without the leading #)
#
# [Service]
# Environment="TMPDIR=/path/to/new/tmp"
# Environment="PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR=/home/myusername/Library/Application Support"
# User=myusername
# Group=mygroup
#

[Unit]
Description=Plex Media Server
After=network.target network-online.target

[Service]
Environment="PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR=/var/lib/plexmediaserver/Library/Application Support"
Environment=PLEX_MEDIA_SERVER_HOME=/usr/lib/plexmediaserver
Environment=PLEX_MEDIA_SERVER_MAX_PLUGIN_PROCS=6
ExecStartPre=/bin/sh -c '/usr/bin/test -d "${PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR}" || /bin/mkdir -p "${PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR}"'
ExecStart=/bin/sh -c '\
export PLEX_MEDIA_SERVER_INFO_VENDOR="$(grep ^NAME= /etc/os-release | awk -F= "{print \\$2}" | tr -d \\" )"; \
export PLEX_MEDIA_SERVER_INFO_DEVICE="PC"; \
export PLEX_MEDIA_SERVER_INFO_MODEL="$(uname -m)"; \
export PLEX_MEDIA_SERVER_INFO_PLATFORM_VERSION="$(grep ^VERSION= /etc/os-release | awk -F= "{print \\$2}" | tr -d \\" )"; \
exec "/usr/lib/plexmediaserver/Plex Media Server"'
Type=simple
User=plex
Group=plex
Restart=on-failure
RestartSec=5
StartLimitInterval=60s
StartLimitBurst=3
SyslogIdentifier=Plex Media Server
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target

Directories and Files

On Linux, Plex Media Server uses the following directories and files by default. These are defined by the cgroup information shown above.

Programs /usr/lib/plexmediaserver/
Service Configuration /usr/lib/plexmediaserver/lib/plexmediaserver.service
Content library metadata /var/lib/plexmediaserver/Library/Application Support/Plex Media Server/

Adjusting CPU Priority

By default, the Plex processes run at nice factor 35 (priority 20), which is standard. When transcoding this means that most other processes, such as web servers, must wait. Transcoding can take a while, which means that the websites served from the same server time out.

There are several ways to control how much CPU resource is allocated to a Linux process. I ran my Plex processes under nice so other processes can continue unimpeded. Nice only throttles the process it manages when other processes request a time slice. If the system load is low then nice does not throttle the process that is supervises.

All the Plex processes run under Linux as user plex, group plex. It is easy to change the priority of these processes using nice. The following changes all currently running Plex processes to priority 22:

Shell
$ sudo renice 22 -u plex

I noticed that the video stream would stall at this priority, so I raised it by 10% (10% "less nice" equates to renicing by -2). Aaron Kili wrote a nice explanation of renice.

Shell
$ sudo renice -2 -u plex

One way to ensure that Plex Media Server always runs at normal priority is to add this to the root crontab:

sudo crontab -e
@reboot sleep 60 && renice 18 -u plex

Rygel

While playing with Plex on Ubuntu I noticed that rygel became active when Plex started transcoding. Coincidence? I did not know about rygel before. The GitHub repo.

* indicates a required field.

Please select the following to receive Mike Slinn’s newsletter:

You can unsubscribe at any time by clicking the link in the footer of emails.

Mike Slinn uses Mailchimp as his marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp’s privacy practices.