Plex is an amazing piece of software. One issue with Plex Media Server, though, is that it doesn't auto-update the library when there are changes on connected network shares. This can be annoying if you run your PMS on a different machine (e.g. Nvidia Shield, Mac Mini, etc) than where your media library is stored (e.g. NAS), as you either have to trigger an update on PMS manually when adding/moving/renaming content or wait for the library update interval to kick in.
So, I wrote a script to automate that (borrowed heavily from here).
The script works by tying into inotify on the NAS, which is a kernel subsystem that notices changes to the filesystem and reports those changes to applications. The script uses inotify to monitor the media directories on the NAS for changes, then connects to the remote Plex Server's web API to find the appropriate media section to refresh. If it finds a matching section, it uses the web API to send an update command to that section.
Make sure you have "Python3" installed.
You'll need to install the "pynotify" Python module. The easiest way is to install the Python EasyInstall utility; Shell into your server, and run:
wget https://bootstrap.pypa.io/ez_setup.py -O - | python3then run:
plex-inotify.pyscript somewhere on your NAS/fileserver, e.g.
plex_server_hostvariable near the top of your script to match the IP address of your Plex Server. If you have local DNS resolution, you can use a hostname instead.
path_mapsvariable to map the local paths of the media shares on your fileserver to their corresponding library names in your Plex Media Server.
You should change the
Falsefor testing purposes until you're sure that everything is working properly.
Try running the script with
python3 plex-inotify.py, and if all goes well, it will load up without errors :)
If you set
True, then the script will fork itself into a background task when you run it. It will stay running even if you log out of the shell.
If you see a bunch of errors that say something like
Errno=No space left on device (ENOSPC), then your inotify watcher limit is too low. Run
sysctl -n -w fs.inotify.max_user_watches=16384and then try again. Keep raising the number until the errors go away.
If you see an error that says
Errno=No such file or directory (ENOENT), then you didn't configure your
paths_mapsproperly. Make sure each entry in the list is a local path to your media on the NAS and then the corresponding library/section name on your PMS.
If you're getting an error that says
urllib.error.HTTPError: HTTP Error 401: Unauthorized, then you need to set the
plex_account_tokenvariable. Follow this link for instructions on how to get your account token. Make sure that when you're setting the variable, you wrap the token in quotes, like this:
plex_account_token = 'A2ekcFXjzPqmefBpv8da'
Let me know if you find this useful!