How to I properly prevent systemd suspend using a script in /lib/systemd/system-sleep/

Questions about applications and software
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
arm3112
Level 1
Level 1
Posts: 6
Joined: Sat Sep 24, 2022 1:26 pm

How to I properly prevent systemd suspend using a script in /lib/systemd/system-sleep/

Post by arm3112 »

I'm fairly new to Linux and trying to learn. I'm using Plex Media Server and I'm trying to prevent the system from sleeping while streaming a file. I've searched the internet over the last few days and none of the solutions seem to work. One solution I feel is almost getting me there, but it's not quite working.

Here is my system information:

Code: Select all

System:
  Kernel: 5.15.0-48-generic x86_64 bits: 64 compiler: gcc v: 11.2.0 Desktop: Cinnamon 5.4.12
    tk: GTK 3.24.33 wm: Mutter dm: LightDM Distro: Linux Mint 21 Vanessa base: Ubuntu 22.04 jammy
Here is the script I've placed (and made executable) in /lib/systemd/system-sleep/ (based on this site).

Code: Select all

#!/bin/bash

PATH=/sbin:/usr/sbin:/bin:/usr/bin
X_DISPLAY_USERNAME=myusername

plexresume()
{
    number_of_sessions=$(curl -s localhost:32400/status/sessions? | sed -n "s/.*MediaContainer size=\"\(.*\)\".*/\1/p")
    if [ ${number_of_sessions} -gt 0 ]; then
        echo "[$(date +"%Y.%m.%d-%T")] Number of streamers = ${number_of_sessions} Plex session active, cancel suspend" >> /tmp/plex_sleep_log
        return 1
    else
        echo "[$(date +"%Y.%m.%d-%T")] Number of streamers = ${number_of_sessions} Plex session -IN-active, going to sleep now   zzzzzzzz........" >> /tmp/plex_sleep_log
        return 0
    fi
}

plexkeepalive()
{
    echo "[$(date +"%Y.%m.%d-%T")] Resuming!!..." >> /tmp/plex_sleep_log
    su ${X_DISPLAY_USERNAME} -c "DISPLAY=:0 /usr/bin/xdotool getmouselocation | grep 'x:1 y:1 '" > /dev/null
    if [ "$?" == "0" ]; then
        su ${X_DISPLAY_USERNAME} -c "DISPLAY=:0 /usr/bin/xdotool mousemove 9 9"
    else
        su ${X_DISPLAY_USERNAME} -c "DISPLAY=:0 /usr/bin/xdotool mousemove 1 1"
    fi
    return 0
}

case "$1" in
    pre)
        plexresume
        ;;
    post)
        plexkeepalive
        ;;
esac
I know it's executing because it's printing to the log file. But even when it's printing that Plex is active, it's still suspending the system. I've manually run the script using sudo outside of systemd and checking the value of $? after, which is 1.

When I use

Code: Select all

journalctl -b -u systemd-suspend.service
I see the following:

Code: Select all

Sep 26 12:47:03 systemname systemd[1]: Starting System Suspend...
Sep 26 12:47:03 systemname [141890]: /usr/lib/systemd/system-sleep/plexkeepalive failed with exit status 1.
Sep 26 12:47:03 systemname systemd-sleep[141887]: Entering sleep state 'suspend'...
One time I got a successful result, but I'm not sure how it happened:

Code: Select all

Sep 26 10:51:45 systemname systemd-sleep[112491]: Entering sleep state 'suspend'...
Sep 26 10:52:25 systemname systemd-sleep[112491]: Failed to put system to sleep. System resumed again: Device or resource busy
Sep 26 10:52:25 systemname su[112556]: (to myusername) root on none
Sep 26 10:52:25 systemname su[112556]: pam_unix(su:session): session opened for user myusername(uid=1000) by (uid=0)
Sep 26 10:52:25 systemname su[112556]: pam_unix(su:session): session closed for user myusername
Sep 26 10:52:25 systemname su[112592]: (to myusername) root on none
Sep 26 10:52:25 systemname su[112592]: pam_unix(su:session): session opened for user myusername(uid=1000) by (uid=0)
Sep 26 10:52:25 systemname su[112592]: pam_unix(su:session): session closed for user myusername
Sep 26 10:52:25 systemname systemd[1]: systemd-suspend.service: Main process exited, code=exited, status=1/FAILURE
Sep 26 10:52:25 systemname systemd[1]: systemd-suspend.service: Failed with result 'exit-code'.
Sep 26 10:52:25 systemname systemd[1]: Failed to start System Suspend.
Sep 26 10:52:25 systemname systemd[1]: systemd-suspend.service: Consumed 2.732s CPU time.
I've also since tried this by doing just 'exit 1' in the 'pre' case, and it still returns the same error, and still suspends.

Any help on this issue would be appreciated. I don't understand why returning 1 from the script is not preventing systemd-suspend.service from running. Thank you!
Last edited by LockBot on Sun Mar 26, 2023 10:00 pm, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
arm3112
Level 1
Level 1
Posts: 6
Joined: Sat Sep 24, 2022 1:26 pm

Re: How to I properly prevent systemd suspend using a script in /lib/systemd/system-sleep/

Post by arm3112 »

Out of persistence, I ended up writing a bash script that checks the number of active plex streams every 5 minutes using a cron job, and if active, it will jiggle the mouse to keep the computer awake. It writes to a temporary file, and after 3 consecutive periods of the mouse not moving, it will turn on the screensaver. It ended up working pretty well. If anyone is interested in that script I can post it.
Locked

Return to “Software & Applications”