PLEX server should keep Mac awake after wake-on-lan

hello,

 

i have the following issue:

 

i have the latest plex server up and running on my mac mini (late 09, 10.9.2). everything works fine. the mac mini is usually in sleep mode. when i am at home, my logitech harmony wakes the system directly into plex home theater. so far so good.

 

for the scenario that i am not at home but i want to stream a movie to my iphone/notebook i have set up wake-on-lan on my mac mini. this actually works! even wake-over-internet works great. but there is a crucial issue: after exactly 1 minute the mac mini goes back into sleep mode. this even happens if i have already started a stream. the stream stops, the library goes offline and the mac mini is back in sleep mode.

 

i have been told that this is a normal behaviour. after the mac mini receives a wake-on-lan package, it wakes up and listens to services such as SSH or remote login. if there is no response the mac goes back into sleep mode.

 

my suggestion or better said, request: the plex server should keep the mac awake as soon as a connection has been established. 

 

or is there any other nice way to keep the make awake after a wake-on-lan attempt?

 

thank you very much in advance!

i have found a workaround:

* edit as admin:

/Library/Preferences/SystemConfiguration/com.apple.Boot.plist

* change:

Kernel Flags


to:

Kernel Flags
darkwake=0

* restart 

this disables the "darkwake" funktion from mac. and the system is _really_ awake and stays that way.

This solution no longer works in El Capitan because com.apple.Boot.plist cannot be changed.

I found a solution that allows my Mac Mini to sleep normally, unless my Plex server is being accessed. It works by putting Plex in debug logging mode, and then checking the Plex log file every 30 seconds to see if it has changed. If so, it assumes someone is accessing Plex so it runs caffeinate to prevent the Mac from sleeping for 5 minutes.

Note that if the Plex server is not being accessed, then no new log messages will be written to the Plex logs, so caffeinate will not be run, so your Mac will sleep as normal!

  1. Create ~/Library/LaunchAgents/PlexCaffeinate.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>PlexCaffeinate</string>

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

    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
</dict>
</plist>
  1. Create ~/Library/Scripts/PlexCaffeinate.sh as follows:
#!/bin/sh

defaults write com.plexapp.plexmediaserver logDebug true

plexLog=~/Library/Logs/Plex\ Media\ Server/Plex\ Media\ Server.log
caffeinateLog=~/Library/Logs/Plex\ Media\ Server/Plex\ Caffeinate.log

prevLastModified=`stat -f %m "$plexLog" 2>/dev/null`
while true; do
    lastModified=`stat -f %m "$plexLog" 2>/dev/null`
    if [ "$prevLastModified" = "$lastModified" ]; then
        sleep 30
    else
        echo "`date` Caffeinate..." >> "$caffeinateLog"
        caffeinate -s -t 300 &
        sleep 299
        echo "`date` Caffeinated." >> "$caffeinateLog"
    fi
    prevLastModified=$lastModified
done
  1. In Terminal, run the following:
chmod a+x ~/Library/Scripts/PlexCaffeinate.sh

launchctl load ~/Library/LaunchAgents/PlexCaffeinate.plist
1 Like

@Parnty

Does it work with 10.11.6?
I’m completly newby in such things. For example you write “create” - with what App/Programm? Could you do step by step for dummies?

thx you very much

Here’s a step-by-step guide. The main issue is that directory ~/Library is hidden by default in Mac OS X, so is not easy to access from Finder. So you’ll run all these commands from Terminal.

  1. From the Dock, run Launchpad > Other > Terminal

  2. In the Terminal window that pops up, copy-paste the following (and hit return):

echo "" >> ~/Library/LaunchAgents/PlexCaffeinate.plist && open -a TextEdit ~/Library/LaunchAgents/PlexCaffeinate.plist
  1. In the empty TextEdit window that pops up, copy-paste the text from the box in item (1) in my earlier post above.

  2. In the TextEdit menu bar, click File > Save, then File > Close

  3. Go back to the Terminal window, and copy-paste the following (and hit return):

[ -d ~/Library/Scripts ] || mkdir ~/Library/Scripts
  1. In the same Terminal window, copy-paste the following (and hit return):
echo "" >> ~/Library/Scripts/PlexCaffeinate.sh && open -a TextEdit ~/Library/Scripts/PlexCaffeinate.sh
  1. In the empty TextEdit window that pops up, copy-paste the text from the box in item (2) in my earlier post above.

  2. In the TextEdit menu bar, click File > Save, then File > Close

  3. Go back to the Terminal window, and copy-paste the text from the box in item (3) in my earlier post (and hit return).

  4. That’s it. To test that it’s working, in the same Terminal window, copy-paste the following:

echo "" >> ~/Library/Logs/Plex\ Media\ Server/Plex\ Caffeinate.log && tail -f ~/Library/Logs/Plex\ Media\ Server/Plex\ Caffeinate.log
  1. Now leave that Terminal window open, and try accessing Plex and browsing your library (e.g. using your TV’s Plex app, or another computer). Once you’ve done that, go back to the Terminal window, and you should see the following message (after about 30 seconds):
Fri Sep  2 09:43:57 CEST 2016 Caffeinate...
  1. After 5 minutes, the following message should appear in the Terminal window:
Fri Sep  2 09:48:56 CEST 2016 Caffeinated.

You can now safely quit Terminal (ignore any warnings it gives you). This PlexCaffeinate script will continue running in the background (and will be automatically started when you first login to your Mac).

1 Like

You can still edit com.apple.Boot.plist, but it requires turning off SIP. I don’t know though after editing the file if you can re enable SIP and keep the changes.

My solution has the benefit that there’s no need to disable your Mac’s default power saving features (i.e. darkwake). It does have the following issues, but they’re relatively minor and overall it’s working great for me:

  1. My Plex logs are now larger (but only a few Mb, and they’re automatically discarded)
  2. Since Plex does periodic housekeeping causing the logs to get updated, my Mac sometimes gets kept awake for 5 minutes by my Caffeinate script when I’m not using Plex.

It would be great if Plex had an access log that only got updated when someone browsed the library. Then my script could monitor this for changes, instead of the debug log (solving both issues above).

@Parnty
Thx you for your guide!!! Will try it soon.
One more question - how can it be reverted / deleted if it does not work?
Are there any problems with future OSX or Plex updates?

best regards

You can revert/uninstall it by opening a Terminal window and copy-pasting the following (and hitting return):

launchctl unload ~/Library/LaunchAgents/PlexCaffeinate.plist

rm ~/Library/LaunchAgents/PlexCaffeinate.plist

rm ~/Library/Scripts/PlexCaffeinate.sh

defaults write com.plexapp.plexmediaserver logDebug false

The solution I’ve given should be compatible with all current and future versions of OS X, as it uses pretty fundamental Mac features that are unlikely to change. The only issue I can see is if Plex changes the name/location of their log files in a future release.

Then again, it would be even better if the Plex team would (continue their great work and) build a solution into Plex itself for preventing the Mac from sleeping while a user is browsing their Plex library. Fingers crossed!

1 Like

If this works and my iMac could sleep while not in use with PMS and sleeps again after using it, the energysaving would be great. I have tried wimoweh as well. Doesnt work.

Couldnt the PlexTeam implement this in PMS?

thx

@Parnty

So, thanks to your step by step info even i could do it and it works flawless. Saves energy and money. You are my hero!! Many thanks.
Please keep it up to date if any changes in OSX OR PMS show up. Im a happy man.

@Plex
Please implement this in OSX PMS. It works and saves money.

best regards

Glad to hear it :slight_smile: Yes, if any changes are required in future, I’ll post a message here.

thank you.

@Parnty Thank you very much for the scripts, it works great and was exactly what I needed :wink:

@Parnty, also a big thanks from me, been struggling with this for some time!

@Parnty
Have you tried your script with sierra? Does it work? Still waiting to update because i dont wanr a second El Capitan release.

best regards

So far ok.
Best regards

Thanks for the script… but it kinda works for me…after it activates it doesn’t stop automatically after i close plex. and my mac is not going to sleep after that, even when i try to put in manually to sleep , i have to stop the caffeine process in activity monitor.
Help would be appreciated
thanks

works for me. works like it should. did exact the step by step guide. im on sierra and works. 5 min after last plex activity + after 10 min sleep in energy settings than my iMac sleeps like a baby. try to contact parnty

It’s possible your Plex server is doing housekeeping tasks in the background, and therefore writing messages to its log file. In this case, my script will assume someone is using Plex, so will run caffeinate to prevent your Mac from sleeping. Check this as follows:


  1. From the Dock, click Launchpad > Other > Console

  2. In the Console application, on the left-hand side click the triangle to expand ~/Library/Logs then Plex Media Server then click on Plex Media Server.log (make sure you click the file with this exact name)

  3. If you see new messages continuously appearing in the right-hand pane, then this is the cause of the problem.


Your Plex Server may be doing a one-off (but long-running) housekeeping task. Once this has finished, your Mac will be able to sleep again. So you could just wait for a day or so.

You should also make sure your Plex Server is updated to the latest version.