-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathplexscan.sh
executable file
·66 lines (56 loc) · 2.64 KB
/
plexscan.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash
## PLEX SCAN ONLY NEW/MODIFED FOLDERS SINCE LAST RUN
## OS: Linux Ubuntu 16.04
## Make script executable by chmod a+x plexscan.sh
## Add script to crontab -e ( paste the line bellow without ## )
## */30 * * * * /path to script/plex-scan-new.sh >/dev/null 2>&1
## Make sure you disable all Plex automatic & scheduled library scans.
## Credit to https://github.com/ajkis for the original script.
if pidof -o %PPID -x "$0"; then
echo "$(date "+%d.%m.%Y %T") Exit, already running."
exit 1
fi
#SETTINGS
MOVIELIBRARY="/home/kamos/cloud/movies"
MOVIESECTION=2
TVLIBRARY="/home/kamos/cloud/tv"
TVSECTION=1
LOGFILE="/home/kamos/logs/plexscan.log"
FOLDERLISTFILE="/home/kamos/.cache/folderlistfile"
LASTRUNFILE="/home/kamos/.cache/lastrunfile"
export LD_LIBRARY_PATH=/usr/lib/plexmediaserver
export PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR=/var/lib/plexmediaserver/Library/Application\ Support
if [[ ! -f "$LASTRUNFILE" ]]; then
touch $LASTRUNFILE
fi
echo "$(date "+%d.%m.%Y %T") PLEX SCAN FOR NEW/MODIFIED FILES AFTER: $(date -r $LASTRUNFILE)" | tee -a "$LOGFILE"
if [[ -f "$FOLDERLISTFILE" ]]; then
echo "Removing previous folder list" | tee -a "$LOGFILE"
rm $FOLDERLISTFILE
fi
start=$(date +'%s')
startmovies=$(date +'%s')
echo "Scaning for new files: $MOVIELIBRARY" | tee -a "$LOGFILE"
find "$MOVIELIBRARY" -mindepth 1 -type d -cnewer $LASTRUNFILE |
while read mfile; do
echo "$(date "+%d.%m.%Y %T") New file detected: $mfile" | tee -a "$LOGFILE"
# MFOLDER=$(dirname "${mfile}")
# echo "$MFOLDER" | tee -a "$FOLDERLISTFILE"
$LD_LIBRARY_PATH/Plex\ Media\ Scanner -s -r -c "$MOVIESECTION" -d "$mfile"
done
echo "$(date "+%d.%m.%Y %T") Movie files scanned in $(($(date +'%s') - $startmovies)) seconds" | tee -a "$LOGFILE"
startseries=$(date +'%s')
echo "Scaning for new files: $TVLIBRARY" | tee -a "$LOGFILE"
find "$TVLIBRARY" -mindepth 2 -type f -cnewer $LASTRUNFILE |
while read tvfile; do
echo "$(date "+%d.%m.%Y %T") New file detected: $tvfile" | tee -a "$LOGFILE"
TVFOLDER=$(dirname "${tvfile}")
echo "$TVFOLDER" | tee -a "$FOLDERLISTFILE"
$LD_LIBRARY_PATH/Plex\ Media\ Scanner -s -r -c "$TVSECTION" -d "$TVFOLDER"
done
echo "$(date "+%d.%m.%Y %T") TV folders scanned in $(($(date +'%s') - $startseries)) seconds" | tee -a "$LOGFILE"
echo "$(date "+%d.%m.%Y %T") Move & TV folders scanned in $(($(date +'%s') - $start)) seconds" | tee -a "$LOGFILE"
echo "$(date "+%d.%m.%Y %T") Setting lastrun for next folder scans" | tee -a "$LOGFILE"
touch $LASTRUNFILE
echo "------------------------------------------------------------------------------------------------------------" | tee -a "$LOGFILE"
exit