Plex Media Server auto restart on crash (MacOS)

Summary

If you use the Plex Media Server on your home PC to serve your media content, then you know how important it is to keep that service up and running at all times. For this reason I found it frustrating when my iOS Plex Player kept crashing the server.

For some reason, the iOS Plex App would crash the server anytime I tried to play a video using automatic quality throttling. Anyway, after I figured out it was the iOS App, I began looking into way to make sure that in the future, the server would come back up, even if it had been crashed.

Steps

Setting up a process to auto-restart is simple. You just need to create a LaunchAgent and have LaunchD (MacOS’s agent and daemon controller) take care of the rest:

  1. Remove the check on Plex Media Server to Open at Login
    1. If you Plex Media Server is already running, go up to the menu bar and make sure to uncheck Open at Login. Otherwise, you could end up with duplicate processes.
  2. Create a file like this one (if you are using Plex Media Server.app, you can just use that file) and make sure it’s named com.plexapp.plexmediaserver.plist
    1. Lines 5 to 11 tell launchd to start the program when computer starts and to restart it (keepAlive) if it crashes.
    2. Lines 11 and 12 give a name to the LaunchAgent.
    3. Lines 16 to 18 are the program’s parameters. open is a built-in program in MacOS for opening files, URLs and programs. -g tells open to start the program in the background. And /Applications/Plex Media Server.app is the path to our application 🙂
  3. Place the file under the LaunchAgents folder in the user’s Library (here: ~/Library/LaunchAgents/). You should end up with this path: /Users/<your username>/Library/LaunchAgents/com.plexapp.plexmediaserver.plist
  4. Load the LaunchAgent into LaunchD like this: launchctl load ~/Library/LaunchAgents/com.plexapp.plexmediaserver.plist
    1. That tells LaunchD to look at the configuration file in the plist file you have given it and execute it
    2. You can also unload it, which means to remove the plist file from LaunchD’s queue of things to control. launchctl unload ~/Library/LaunchAgents/com.plexapp.plexmediaserver.plist

That’s it. From now on, when the computer logs in, it will start Plex Media Server and if it crashes and it will automatically restart.

Troubleshooting

If you run into issues, you can troubleshoot by looking at the system logs and loading or unloading the plist file.

The system.log records any error messages generated by LaunchAgents (not just the one you just created). You can view the system log like this:

  • To just view what is there now: cat /var/log/system.log
  • To scroll through what is there now: cat /var/log/system.log | more (and press the spacebar to progress)
  • To view the most recent changes in real-time: tail -f /var/log/system.log
    • I find this to be the most helpful

With access to the system log, you can now test your plist by

  1. Unloading the plist
  2. Edit the plist
  3. Re-Load the plist
  4. Check the system log

 

Leave a Reply

Your email address will not be published. Required fields are marked *