Jump to content

Sleep prevention not working


M1tch

Recommended Posts

M1tch

I saw this a few times on the windows server and ended up just setting sleep in the os to 3 hrs.

 

The Mac appears to do the same I have tried 15 and 30 mins and each time the server sleeps mid stream to a Samsung tv .

 

Anything else I check ?

Link to comment
Share on other sites

Hi, on MacOS we don't currently have any sleep prevention so you'll need to configure it in system settings. It's something we plan to add in the future. Thanks.

Link to comment
Share on other sites

M1tch

For anyone searching for similar, here is a stop gap that works pretty well until the Emby team add it in the future. I can't take credit I just reused from a cool script from "Parnty" here https://forums.plex.tv/t/plex-server-should-keep-mac-awake-after-wake-on-lan/60590/3

 

 

It checks the last 20 lines of the Emby Server log file for "emby/Sessions/Playing/Progress" every 30 secs and uses caffeinate to keep the mac awake.

When it can't see that string the assertion gets removed and the mac sleeps per the system settings.

 

Note: I may try to change this to look for any update to the log file as if emby is updating metadata etc the playing/progress piece gets lost in the noise. If anyone has a better way to approach i'd been keen to hear as i'm no dev..

 

 

1. Create ~/Library/LaunchAgents/embyCaffeinate.plist as follows:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>embyCaffeinate</string>

    <key>ProgramArguments</key>
    <array>
        <string>sh</string>
        <string>-c</string>
        <string>"$HOME/Library/Scripts/embyCaffeinate.sh"</string>
    </array>

    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
</dict>
</plist>

2. Create ~/Library/Scripts/embyCaffeinate.sh as follows:

#!/bin/bash


# Paths
embyLog=~/emby-server/logs/embyserver.txt 
caffLog=~/emby-server/logs/Caffeinate.log

# Kill subprocess on exit
trap '[[ -z "$(jobs -p)" ]] || kill $(jobs -p)' EXIT

# Functions
timestamp()      { date "+%b %d %H:%M:%S"; }
today()          { date "+%d"; }
embyUpdateTime() { stat -f %m "$embyLog" 2>/dev/null; }
embyUserAction() { tail -20 "$embyLog" | \
                   grep "emby/Sessions/Playing/Progress" | \
                   tail -1 2>/dev/null; }

# Init globals
initDay()        { day=$(today); }
initembyState()  { updateTime=$(embyUpdateTime);
                   userAction=$(embyUserAction); }

# Day check
isSameDay()
{
    local myDay=$(today)
    if [ "$myDay" = "$day" ]; then
        return 0
    else
        day=$myDay 
        return 1
    fi
}

# Idle check
isembyIdle()
{
    local myUpdateTime=$(embyUpdateTime)
    if [ -z "$myUpdateTime" ] || \
       [ "$myUpdateTime" = "$updateTime" ]; then
        return 0
    else
        updateTime=$myUpdateTime
        return 1
    fi
}

# Housekeeping check
isembyHousekeeping()
{
    local myUserAction=$(embyUserAction)
    if [ -z "$myUserAction" ] || \
       [ "$myUserAction" = "$userAction" ]; then
        return 0
    else
        userAction=$myUserAction
        return 1
    fi
}

# Wait until user has finished using emby
waitForUser()
{
    initembyState
    while true; do
        echo "$(timestamp) Caffeinating..." >> "$caffLog"

        sleep 120

        if isembyIdle;         then break; fi
        if isembyHousekeeping; then break; fi
    done
}

# Check emby and caffeinate if needed
initDay
initembyState
while true; do
    sleep 30

    # Rotate logs
    if ! isSameDay; then mv "$caffLog" "$caffLog".1 2>/dev/null; fi

    # Check emby
    if isembyIdle; then
        echo "$(timestamp) emby is idle" >> "$caffLog"
    elif isembyHousekeeping; then
        echo "$(timestamp) emby is running background tasks" >> "$caffLog"

    # Caffeinate
    else
        echo "$(timestamp) Caffeinate" >> "$caffLog"
        waitForUser &
        caffeinate -is -w $! 2>/dev/null
        echo "$(timestamp) Caffeinated" >> "$caffLog"

        initembyState
    fi
done

 

3.In Terminal, run the following

chmod a+x ~/Library/Scripts/embyCaffeinate.sh

launchctl load ~/Library/LaunchAgents/embyCaffeinate.plist
Edited by M1tch
Link to comment
Share on other sites

  • 2 weeks later...
  • 3 years later...
  • 5 weeks later...
On 1/14/2023 at 12:51 PM, macv said:

Is sleep prevention working?
Seemed to go to sleep in the middle of playback on my M1 Mackbook Pro.

Hi, do you have wake for network access enabled on your mac?

https://support.apple.com/en-in/guide/mac-help/mh27905/mac#:~:text=Mac desktop computer-,On your Mac%2C choose Apple menu > System Settings%2C then,may need to scroll down.)&text=Turn on “Wake for network access” on the right.

Link to comment
Share on other sites

  • 2 weeks later...
On 2/13/2023 at 2:38 PM, Luke said:

Yup, it's enabled, seems to have been on by default. For now I still have to run `caffeinate` while playing.

Link to comment
Share on other sites

22 hours ago, macv said:

Yup, it's enabled, seems to have been on by default. For now I still have to run `caffeinate` while playing.

What version of macOS are you on?

Link to comment
Share on other sites

Also, try playing something from an Emby app, and then while it's playing, enter this into the macOS terminal:

pmset -g assertions

That will list the processes that are keeping the system awake. It may take up to five minutes from when playback starts until Emby Server gets listed there.

Link to comment
Share on other sites

MacOS 13.2.1 (22D68)

`pmset -g assertions` did show emby strangely still shut down if I don't also run caffeinate

   pid 71684(EmbyServer): [0x0004b92f0001a534] 00:21:58 PreventUserIdleSystemSleep named: "Emby Server has active sessions"

 

Link to comment
Share on other sites

I like to run it with the -d option so it says 

   pid 95536(caffeinate): [0x0004fbf50005a116] 00:00:06 PreventUserIdleDisplaySleep named: "caffeinate command-line tool"
	Details: caffeinate asserting forever
	Localized=THE CAFFEINATE TOOL IS PREVENTING SLEEP.

Without -d, it says the same thing as emby:

   pid 95150(caffeinate): [0x0004fad20001a058] 00:00:02 PreventUserIdleSystemSleep named: "caffeinate command-line tool"

Haven't tested emby playback without the -d, might be worth trying for someone (I'm traveling today & out of action for next few weeks).

Edited by macv
Link to comment
Share on other sites

2 hours ago, Luke said:

When it sleeps, how long does it take to do so?

around an hour, laptop is always plugged in

Link to comment
Share on other sites

  • 3 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...