killScan.sh 702 B

12345678910111213141516171819202122232425262728293031
  1. #!/bin/bash
  2. # Find all scanner processes
  3. PROCS="`ps -A -o "pid,lstart,command" | grep -v grep | grep 'Plex Media Scanner'`"
  4. if [ -z "${PROCS}" ]; then
  5. exit
  6. fi
  7. IFS=''
  8. echo "${PROCS}" | \
  9. while read -r PROC; do
  10. # Parse the line
  11. PID="`echo "${PROC}" | awk '{print $1}'`"
  12. DATE="`echo "${PROC}" | awk '{print $2, $3, $4, $5, $6}'`"
  13. TS="`date -j -f '%a %b %d %T %Y' "${DATE}" '+%s' 2>/dev/null`"
  14. NOW="`date '+%s'`"
  15. # Deal with parsing errors
  16. if [ -z "${TS}" ]; then
  17. echo "Invalid date: ${DATE}" 1>&2
  18. echo "Full line: ${PROC}" 1>&2
  19. continue
  20. fi
  21. # Kill scanners more than 30 minutes old
  22. AGE=$(( $NOW - $TS ))
  23. if [ $AGE -gt 1800 ]; then
  24. echo "Killing ${PROC}"
  25. kill -9 "${PID}"
  26. fi
  27. done