Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # Update 4182020: Changed all console outputs to instead write to a logfile. The logfile will
- # always be in the scripts directory, regardless of where script is executed from.
- # This is a script that checks for the /dev/dri device existing inside the Plex jail.
- # If it does not exist, it kills the jail, restarts devfs service, and restarts the jail.
- # If there are open streams, the script waits 5 minutes and checks again. After checking 12 times,
- # the script will exit. When there are 0 streams open, the script continues with
- # the check.
- # To email and wipe the logfile, use this as a cron task:
- # mail -s "Plex DRI Check Log" EmailAddress@here.com < /path/to/log/plex_dri_check.log; echo "" > /path/to/log/plex_dri_check.log
- # Specify Plex jail name, IP, port, auth token, and custom ruleset number:
- PlexName="plex"
- PlexIP="192.168.1.100"
- PlexPort="32400"
- Token="replace_this_with_token"
- RuleNum="110"
- TimeStamp()
- {
- date +"%Y-%m-%d %T:"
- }
- TimeStampPre() {
- while IFS= read -r line; do
- printf '%s %s\n' "$(date +"%Y-%m-%d %T:")" "$line";
- done
- }
- LogFile="${0%/*}/plex_dri_check.log"
- RetryCount=0
- StreamCount=$(curl "http://$PlexIP:$PlexPort/status/sessions?X-Plex-Token=$Token" --stderr - \
- | grep 'MediaContainer size=' | grep -o '[0-9]\+')
- while [ $StreamCount -gt 0 ]
- do
- echo "$(TimeStamp) There are currently $StreamCount open stream(s). Sleeping for 5m, then checking again." >> "$LogFile"
- RetryCount=$((RetryCount+1))
- if [ $RetryCount = "12" ]
- then
- echo "$(TimeStamp) Checked $RetryCount times. Stream(s) still open. Exiting." >> "$LogFile"
- exit 0
- fi
- sleep 300
- StreamCount=$(curl "http://$PlexIP:$PlexPort/status/sessions?X-Plex-Token=$Token" --stderr - \
- | grep 'MediaContainer size=' | grep -o '[0-9]\+')
- done
- if iocage exec $PlexName ls /dev/ | grep -c "dri" > /dev/null
- then
- echo "$(TimeStamp) DRI pass-through still exists. Exiting." >> "$LogFile"
- else
- echo "$(TimeStamp) DRI pass-through no longer exists. Killing Plex, restarting devfs, restarting Plex." >> "$LogFile"
- iocage stop $PlexName | TimeStampPre >> "$LogFile"
- sleep 3
- service devfs restart | TimeStampPre >> "$LogFile"
- sleep 3
- iocage start $PlexName | TimeStampPre >> "$LogFile"
- if iocage get all $PlexName | grep -c "ruleset:$RuleNum" > /dev/null
- then
- echo "$(TimeStamp) devfs_ruleset:$RuleNum successfully loaded." >> "$LogFile"
- else
- echo "$(TimeStamp) CRITICAL: devfs_ruleset:$RuleNum failed to load!" >> "$LogFile"
- fi
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement