Skip to content

Instantly share code, notes, and snippets.

@lloesche
Last active January 10, 2022 22:14
Show Gist options
  • Save lloesche/1b878e2a2dfe8a66111e to your computer and use it in GitHub Desktop.
Save lloesche/1b878e2a2dfe8a66111e to your computer and use it in GitHub Desktop.
plexscan.bash
#!/bin/bash
set -o errexit -o nounset -o pipefail
export PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR="/var/lib/plexmediaserver/Library/Application Support"
export PLEX_MEDIA_SERVER_HOME=/usr/lib/plexmediaserver
export PLEX_MEDIA_SERVER_MAX_PLUGIN_PROCS=6
export PLEX_MEDIA_SERVER_TMPDIR=/tmp
export LD_LIBRARY_PATH="$PLEX_MEDIA_SERVER_HOME"
export LC_ALL="en_US.UTF-8"
export LANG="en_US.UTF-8"
ulimit -s 3000
function -h {
cat <<USAGE
USAGE: $0 [options] <section-name>*
Scans the section and refreshes metadata.
Options:
-l List section names
-h Print this help
USAGE
}; function --help { -h ;}
function -l {
scanner --list | cut -d : -f 2 | sort
}
function scanner {
"${PLEX_MEDIA_SERVER_HOME}/Plex Media Scanner" "$@"
}
function sectionId {
scanner -l | \
awk -v section="$1" '\
BEGIN { FS=OFS=":" }
{
gsub(/^[ \t]+|[ \t]+$/, "", $1);
gsub(/^[ \t]+|[ \t]+$/, "", $2);
if($2==section) print $1
}'
}
function scan {
scanner --scan --refresh --section $1
}
function main {
[[ -z "$@" ]] && --help
for section in "$@"
do
declare -i sectionId=$(sectionId "$section")
if ! [ $sectionId -gt 0 ] ; then
echo "error: could not find section ID for section $section" >&2
continue
fi
echo "scanning section $section with id $sectionId"
scan $sectionId &
done
wait
}
if [[ ${1:-} ]] && declare -F | cut -d' ' -f3 | fgrep -qx -- "${1:-}"
then "$@"
else main "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment