How to scan Plex media from Command Line

How to scan Plex media from Command Line

21st November 2018 0 By George Wou
3 min read

Goals: Scan, analyze and refresh a Plex media server library in Linux using the command line interface.

Plex Media server needs to periodically scan the media files in the library so it knows the attributes of each file, otherwise the files cannot be played.

The usual reason for losing info on a file is when the files are stored in a network share, especially smb shares, following a server restart.

The easiest way to manually analyze the files and refresh metadata is through the web interface, but sometimes access is not possible or fast enough. In these cases it is possible to initiate scans through the command line interface.

 

 Commands

Below are the commands and the initial setup required on most linux distros:

-The following commands needs to be executed by user plex , executing them with root or another elevated user will yield no results-

If Plex installation is ok there should be a plex user:

cat /etc/passwd |grep plex

Enter plex user’s shell:

su plex

Check the sections of the library, needed for later commands:

/usr/lib/plexmediaserver/Plex\ Media\ Scanner --list

In case of this error message:

/usr/lib/plexmediaserver/Plex Media Scanner: error while loading shared libraries: libboost_atomic.so.1.59.0: cannot open shared object file: No such file or directory

you need to pass to the shell environment a variable with the path of plex scanner and verify its presence:

LD_LIBRARY_PATH=/usr/lib/plexmediaserver/lib ; export LD_LIBRARY_PATH
env | grep LD

In case there is a second error message

terminate called after throwing an instance of ‘std::runtime_error’
what(): locale::facet::_S_create_c_locale name not valid
Aborted

the commands are being run by root/sudo and as mentioned above, although it needs a simple locale var

LC_ALL="C"
export LC_ALL

root cannot run plex media scanner tasks so make sure you are running the commands as plex user.

Knowing the section numbers you can now run the following examples:

scan files and refresh metadata on section 1, outputs progress:

/usr/lib/plexmediaserver/Plex\ Media\ Scanner -srp -c 1

analyze files info,cannot be used with refresh metadata:

/usr/lib/plexmediaserver/Plex\ Media\ Scanner --analyze -c 1

refresh metadata:

/usr/lib/plexmediaserver/Plex\ Media\ Scanner --refresh -c 1

 

 

How to automate the commands with a script

These environment variables we pass are being exported to the next child process of our shell,in this case our plex media scanner commands, and upon exiting our user shell the variables defined are lost so we need to add them each time.

root@plex: -> plex@plex:export PATH and execute scanner command -> The scanner is scanning(child process)

Since it is not practical to ssh and run all these commands each time we can create a script:

nano plexanalyze.sh

insert path variable and command into the file:

#!/bin/sh
LD_LIBRARY_PATH=/usr/lib/plexmediaserver/lib ; export LD_LIBRARY_PATH
/usr/lib/plexmediaserver/Plex\ Media\ Scanner --analyze -c 1

make the script executable and change owner to plex user since only plex user can initiate the scan:

chmod +x plexanalyze.sh
chown plex:plex plexanalyze.sh

Now scanning a library is as simple as running plexanalyze.sh as plex:

su plex
/usr/lib/plexmediaserver/plexanalyze.sh

This approach is also useful for integration to automation/monitoring tools like zabbix.

 

Edit: In the latest updates of plex media server it seems that the LD_LIBRARY_PATH has been changed from LD_LIBRARY_PATH=/usr/lib/plexmediaserver to LD_LIBRARY_PATH=/usr/lib/plexmediaserver/lib so the article has been updated with the new path.