systemd unit to stop plex

how can i create a systemd unit file to stop plex? i believe there are 3 services that are started as far as i can tell from ps:

plex     12377  0.3  1.2 670688 104844 ?       Ssl  Nov21  41:13 /opt/plexmediaserver/Plex Media Server
plex     12437  0.2  0.6 339016 55220 ?        Sl   Nov21  34:22 /opt/plexmediaserver/Plex DLNA Server
plex     12440  0.0  0.1 514192 11904 ?        Sl   Nov21   8:09 /opt/plexmediaserver/Plex Tuner Service 

the unit file only has /opt/plexmediaserver/Plex\x20Media\x20Server for the ExecStart option. does this file start all services? which command do i need to run manually to stop plex? i want to create a service file to stop plex before shutting down my server.

-----------------------------------------------
/usr/lib/systemd/system/plexmediaserver.service
-----------------------------------------------
[Unit]
Description=Plex Media Server
After=network.target

[Service]
EnvironmentFile=/etc/conf.d/plexmediaserver
ExecStart=/opt/plexmediaserver/Plex\x20Media\x20Server
Type=simple
User=plex
Group=plex
Restart=on-failure
RestartSec=5
StartLimitInterval=60s
StartLimitBurst=3
StandardOutput=null

[Install]
WantedBy=multi-user.target

PMS itself launches all subprocesses.
When PMS receives the termination signal (systemctl stop plexmediaserver) it catches the signal and terminates all subprocesses and exit.

Please use /etc/systemd/system/plexmediaserver.service.d/override.conf to customize your configuration. I overwrite /lib/systemd/system/plexmediaserver.service with each package install. Anything you do there will be lost.

You are free to customize the systemd Environment= statements, as well as User= and Group=, and place those overrides in the override.conf
You may also override the default UMask.

Customization details are here: https://forums.plex.tv/discussion/286395/customizing-your-plex-configuration
This, plus other tips, are found at the top of this forum in https://forums.plex.tv/categories/linux-tips

when you say PMS, do you mean the following executable “/opt/plexmediaserver/Plex Media Server”? and if so, is this the process looking out for termination signals ( kill -9 )? on distros without systemd it says to use service plexmediaserver stop,stop. do you know what the full path of plexmediaserver is? is that the “/opt/plexmediaserver/Plex Media Server”?

What distro are you using as you’re mixing /opt/plexmediaserver and systemd.

I install PMS (Plex Media Server) in /usr/lib/plexmediaserver

im using arch x64. so my thought process was to create a service file that would run the actual pms executable (not sure where this is located, i though it might be “/opt/plexmediaserver/Plex Media Server”) with the stop option. for example “/opt/plexmediaserver/Plex Media Server stop”. but it looks like it might be easier to just make a service file that runs systemctl stop plexmediaserver. the reason i wanted to do this is because i rebooted my plex server twice without stopping the plex service and my storage disk got corrupt. i could not read any data from it. if i stop plex before shutting down and unmount my storage disk, my disk is ok. im going to include this also on my service file but i can do that. its the plex part that im not sure of. im new to plex so dont have much experience with it.

im getting “/opt/plexmediaserver/Plex Media Server” from the /usr/lib/systemd/system/plexmediaserver.service file from the ExecStart= option.

The service file will tell you where it’s located (the ExecStart= line)

The issue with your disk storage getting corrupted because the OS didn’t terminate and flush buffers is not something I can help with . That is a fundamental, and serious, problem with the OS.

Regarding Aarch as a distro, it’s not supported in any way. One of the reasons is because of the corruption issue you just cited.

thats why i want to stop the plex service prior to reboot. i just need to know which command to run to stop it. if i do systemctl stop plexmediaserver, its like im blindly stopping the service. do you know which command/xecutable is the correct one to stop plex? aka “/opt/plexmediaserver/Plex Media Server” with option stop,start etc…?

My point is:

  1. When systemd is informed to shut down, systemd terminates the services.
  2. If your systemd isn’t performing this function, there is a serious problem outside the scope of what you or I can do.

While you don’t want to hear this, your best option is to change to a distro which works correctly at the systemd level and the corruption will stop

/etc/systemd/system/stop-plexmediaserver.service
------------------------------------------------
[Unit]
Description=Stop Plex Media Server
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target

[Service]
ExecStart=/usr/bin/systemctl stop plexmediaserver.service
Type=oneshot

[Install]
WantedBy=shutdown.target reboot.target halt.target

why or why not systemd does not stop plexmediaserver.service is beyond my control/scope of knowledge, but this service file works for me.

do you mount your storage disk via fstab? i am thinking about adding a requires option to the plexmediaserver.service file to ensure the storage drive is mounted prior to starting plex. the only thing i noticed if the storage disk is not mounted is that plex spits out an error about media not being available. i rather not start it if the storage disk is not mounted.

Point of order / caution

If you wish to override (/etc/systemd/system/plexmediaserver.service.d/override.conf) the service parameters, be advised all maintenance of that file would be your responsibility should changes to /usr/lib/systemd/system/plexmediaserver.service be required as each update / upgrade overwrites the existing master service definition and always will.

thanks for the info. with that i made the following:

created a mount unit to mount my storage disk

/etc/systemd/system/mnt-store.mount
-----------------------------------
[Unit]
Description=Mount storage at /mnt/store

[Mount]
What=/dev/sda1
Where=/mnt/store
Options=defaults
Type=ext4

[Install]
WantedBy=multi-user.target

added an override file. these settings require the storage drive to be mounted before plexmediaserver.service starts.

/etc/systemd/system/plexmediaserver.service.d/override.conf
-----------------------------------------------------------
#
# Custom Plex config
#

[Unit]
Requires=mnt-store.mount
After=mnt-store.mount network.target

then
systemctl daemon-reload

and as mentioned above. this unit stops the plexmediaserver.service on shutdown and reboot so there is no corruption to your storage disk

/etc/systemd/system/stop-plexmediaserver.service 
------------------------------------------------
[Unit]
Description=Stop Plex Media Server
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target

[Service]
ExecStart=/usr/bin/systemctl stop plexmediaserver.service
Type=oneshot

[Install]
WantedBy=shutdown.target reboot.target halt.target

you want to enable this one. systemctl enable stop-plexmediaserver.service