PMSCheck.sh 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/bin/bash
  2. # Config
  3. CURL_TIMEOUT=2
  4. RESTART_DELAY=120
  5. PMS_URL="http://localhost:32400/"
  6. ADMIN_EMAIL="zach@kotlarek.com"
  7. # Command-line config
  8. LOOP=-1
  9. if [ "${1}" ]; then
  10. LOOP="${1}"
  11. fi
  12. # Run at least once, loop if requested
  13. while [ $LOOP -ne 0 ]; do
  14. # State tracking
  15. FAILED=""
  16. # Ask for the last update time from Plex
  17. UPDATE="`curl --silent --max-time "${CURL_TIMEOUT}" "${PMS_URL}" | \
  18. grep 'updatedAt=' | sed 's%^.*updatedAt="\([0-9]*\)".*$%\1%'`"
  19. # If Plex replied, assume thing are workingcheck the update time
  20. if [ -z "${UPDATE}" ]; then
  21. FAILED="HTTP timeout"
  22. fi
  23. # If Plex has failed kill it
  24. if [ -n "${FAILED}" ]; then
  25. ERR_MSG="PMS is non-responsive (${FAILED}). Killing..."
  26. echo "${ERR_MSG}" 1>&2
  27. ~/bin/video/pms/killPMS.sh
  28. if [ $LOOP -lt 1 ]; then
  29. exit 1
  30. elif [ -n "${ADMIN_EMAIL}" ]; then
  31. echo -e "Subject: PMS Restart\n\n${ERR_MSG}" | sendmail "${ADMIN_EMAIL}"
  32. fi
  33. # Give plex a breather to get restarted before we check again
  34. sleep "${RESTART_DELAY}"
  35. # Re-index (in the background)
  36. ~/bin/video/isScanning && \
  37. curl --silent --upload-file /dev/null "${PMS_URL}library/optimize" &
  38. fi
  39. # Sleep for the next loop or exit
  40. if [ $LOOP -gt 0 ]; then
  41. sleep $LOOP
  42. else
  43. exit 0
  44. fi
  45. done