fant0men

Is HandBrake running script

Sep 29th, 2019
133
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # This script checks if HandBrake is running.
  3.  
  4. # If HandBrake is running, then the script runs a while loop,
  5. # that checks on the process every 1 second, until the process stops.
  6. # When the process stops, the script prints the total time of
  7. # the process, as well ass the full command and arguments, to the log.
  8.  
  9. # When encoding videos, it's useful to know how long the encoding takes.
  10. # One option is to run the HandBrakeCLI command like so:
  11. # time HandBrakeCLI <args>
  12.  
  13. # But if for some reason, you forgot to do that, or you've since paused
  14. # and resumed the process, then this script can be used to still keep
  15. # track of the total time of the HandBrakeCLI process.
  16.  
  17. # Creates a variable that contains the name of the log file.
  18. log="${HOME}/is_handbrake.log"
  19.  
  20. # If $log is not a file, create it.
  21. if [[ ! -f $log ]]; then
  22.     touch "$log"
  23. fi
  24.  
  25. # Creates a backup of the $IFS variable, and sets it to newline.
  26. IFS_bak="$IFS"
  27. IFS=$'\n'
  28.  
  29. comm='HandBrakeCLI'
  30.  
  31. pids=( $(ps -C "$comm" -o comm,pid | tail -n +2) )
  32.  
  33. IFS="$IFS_bak"
  34.  
  35. for (( i = 0; i < ${#pids[@]}; i++ )); do
  36.  
  37.     pid_info=( $(echo "${pids[${i}]}" | sed 's/ \+/ /' | cut -d' ' -f1-) )
  38.     name=$(echo "${pid_info[0]}" | tr -d '[:blank:]')
  39.     pid=$(echo "${pid_info[1]}" | tr -d '[:blank:]')
  40.  
  41.     if [[ $name == $comm ]]; then
  42.  
  43.         running="${pid}"
  44.  
  45.         while [[ $running ]]; do
  46.  
  47.             sleep 1
  48.  
  49.             unset -v running
  50.             tmp=( $(ps -p $pid -o pid,time | tail -n +2 | sed 's/ \+/ /' | cut -d' ' -f1-) )
  51.             running=$(echo "${tmp[0]}" | tr -d '[:blank:]')
  52.  
  53.             if [[ $running ]]; then
  54.  
  55.                 time=$(echo "${tmp[1]}" | tr -d '[:blank:]')
  56.                 args=$(ps -p $pid -o args | tail -n +2)
  57.  
  58.                 echo -e "${time}: HandBrake is still running!\n"
  59.             else
  60.                 echo "HandBrake has stopped!"
  61.                 echo -e "\nDURATION: ${time}" | tee --append "$log"
  62.                 echo "COMMAND: ${args}" | tee --append "$log"
  63.             fi
  64.         done
  65.     fi
  66. done
RAW Paste Data