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.
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:
- Remove the check on Plex Media Server to Open at Login
- 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.
- 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
- Lines 5 to 11 tell launchd to start the program when computer starts and to restart it (keepAlive) if it crashes.
- Lines 11 and 12 give a name to the LaunchAgent.
- 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 🙂
- 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
- Load the LaunchAgent into LaunchD like this: launchctl load ~/Library/LaunchAgents/com.plexapp.plexmediaserver.plist
- That tells LaunchD to look at the configuration file in the plist file you have given it and execute it
- 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.
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
- Unloading the plist
- Edit the plist
- Re-Load the plist
- Check the system log