Linux Systemd Stupid Trick Of The Day

Linux Systemd Stupid Trick Of The Day

I was a few weeks ago old when I discovered this silly systemd trick by doing some old fashioned RTFM when troubleshooting an issue that I figured it was just way too good not to share.

Additional Background

Every once in a while for whatever the reasons that may be and even with the proliferation of automation and stateful service management frameworks that currently exist in the Linux world, there still a need to take a peek at the composition of a systemd unit file.

When inspecting a systemd file, this is what I used to do:

# List the status of the service using systemctl
#
[root@plex ~]# systemctl status plexmediaserver.service --no-pager
● plexmediaserver.service - Plex Media Server
     Loaded: loaded (/usr/lib/systemd/system/plexmediaserver.service; enabled; preset: disabled)
     Active: active (running) since Sun 2024-06-02 13:30:20 EDT; 12min ago
    Process: 21813 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: 21816 (Plex Media Serv)
      Tasks: 69 (limit: 48731)
     Memory: 6.6G
        CPU: 34min 7.721s
     CGroup: /system.slice/plexmediaserver.service
             ├─21816 "/usr/lib/plexmediaserver/Plex Media Server"
             ├─21839 "Plex Plug-in [com.plexapp.system]" /usr/lib/plexmediaserver/Resources/Plug-ins-c67dce28e/Framework.bundle/Contents/Resourc…
             ├─21883 "/usr/lib/plexmediaserver/Plex Tuner Service" /usr/lib/plexmediaserver/Resources/Tuner/Private /usr/lib/plexmediaserver/Res…
             ├─21919 "Plex Plug-in [com.plexapp.agents.imdb]" /usr/lib/plexmediaserver/Resources/Plug-ins-c67dce28e/Framework.bundle/Contents/Re…
             ├─26710 "/usr/lib/plexmediaserver/Plex Media Scanner" -C -f "/mnt/plex_data/disney/Disney_Movie_Collection/Disney 1928-1949/1945 - …
             └─26745 "/usr/lib/plexmediaserver/Plex Transcoder" -codec:1 aac -analyzeduration 20000000 -probesize 20000000 -i /mnt/plex_data/tv_…

Jun 02 13:30:20 plex systemd[1]: Starting Plex Media Server...
Jun 02 13:30:20 plex systemd[1]: Started Plex Media Server.

# Then copy + paste the path to the systemd unit based on the status output
#
[root@plex ~]# cat /usr/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
Environment=LC_ALL=en_US.UTF-8
Environment=LANG=en_US.UTF-8
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 '\
PLEX_MEDIA_SERVER_INFO_VENDOR="$(grep ^NAME= /etc/os-release | awk -F= "{print \\$2}" | tr -d \\" )" \
PLEX_MEDIA_SERVER_INFO_MODEL="$(uname -m)" \
PLEX_MEDIA_SERVER_INFO_PLATFORM_VERSION="$(grep ^VERSION= /etc/os-release | awk -F= "{print \\$2}" | tr -d \\" )" \
"/usr/lib/plexmediaserver/Plex Media Server"'
Type=simple
User=plex
Group=plex
Restart=on-failure
RestartSec=5
StartLimitInterval=60s
StartLimitBurst=3

[Install]
WantedBy=multi-user.target

It turns out there is a much more effective way to do this with the systemctl cat <unit name> command:

[root@plex ~]# systemctl cat plexmediaserver.service --no-pager
# /usr/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
Environment=LC_ALL=en_US.UTF-8
Environment=LANG=en_US.UTF-8
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 '\
PLEX_MEDIA_SERVER_INFO_VENDOR="$(grep ^NAME= /etc/os-release | awk -F= "{print \\$2}" | tr -d \\" )" \
PLEX_MEDIA_SERVER_INFO_MODEL="$(uname -m)" \
PLEX_MEDIA_SERVER_INFO_PLATFORM_VERSION="$(grep ^VERSION= /etc/os-release | awk -F= "{print \\$2}" | tr -d \\" )" \
"/usr/lib/plexmediaserver/Plex Media Server"'
Type=simple
User=plex
Group=plex
Restart=on-failure
RestartSec=5
StartLimitInterval=60s
StartLimitBurst=3

[Install]
WantedBy=multi-user.target

A B L - Always Be Learnin'