SHARE
TWEET

Kill Firefox (and Chrome) bash script

a guest Dec 3rd, 2019 114 in 25 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # This script creates an infinite while loop, which checks the available
  3. # RAM every 1 second, and kills Firefox, Chrome, Chromium and
  4. # Tor Browser if less than 1 GB is available. The script will only kill
  5. # the tabs, but not the main window itself, so the application keeps
  6. # running but RAM is still freed up.
  7.  
  8. # Creates a function called 'now', which will print the date and time.
  9. now () { date '+%F %H:%M:%S'; }
  10.  
  11. regex='^[[:space:]]*$'
  12.  
  13. # Creates a function called 'is_chromium', which checks if Chromium
  14. # or Chrome is running.
  15. is_chromium () {
  16.  
  17.     for comm in chrome chromium; do
  18.         pgrep "$comm" | tr -d '[:blank:]'
  19.     done
  20. }
  21.  
  22. # Creates a function called 'kill_chromium', which kills all child
  23. # processes belonging to either Chromium or Chrome.
  24. kill_chromium () {
  25.  
  26.     pid_switch=0
  27.  
  28.     for comm in chrome chromium; do
  29.  
  30.         mapfile -t parent < <(ps -C "$comm" -o ppid | tail -n +2 | tr -d '[:blank:]' | sort -u)
  31.         mapfile -t child < <(ps -C "$comm" -o pid | tail -n +2 | tr -d '[:blank:]')
  32.  
  33.         for (( i = 0; i < ${#child[@]}; i++ )); do
  34.             if [[ ! ${child[${i}]} =~ $regex ]]; then
  35.                 child_pid="${child[${i}]}"
  36.  
  37.                 for (( j = 0; j < ${#parent[@]}; j++ )); do
  38.                     if [[ ! ${parent[${j}]} =~ $regex ]]; then
  39.                         parent_pid="${parent[${j}]}"
  40.  
  41.                         if [[ $parent_pid == $child_pid ]]; then
  42.                             pid_switch=1
  43.                         fi
  44.                     fi
  45.                 done
  46.  
  47.                 if [[ $pid_switch -eq 0 ]]; then
  48.                     echo "SIGKILL: ${child_pid}"
  49.                     kill -9 "$child_pid"
  50.                 else
  51.                     pid_switch=0
  52.                 fi
  53.             fi
  54.         done
  55.     done
  56. }
  57.  
  58. # Creates a function called 'kill_firefox', which kills all child
  59. # processes belonging to either Firefox or Tor Browser.
  60. kill_firefox () {
  61.  
  62.     declare -a pids_tmp pids
  63.  
  64.     for name in 'Web Content' 'WebExtensions'; do
  65.         mapfile -t pids_tmp < <(pgrep -x "$name")
  66.         pids+=( "${pids_tmp[@]}" )
  67.     done
  68.  
  69.     for name in firefox tor; do
  70.         for (( i = 0; i <= ${#pids[@]}; i++ )); do
  71.             pid="${pids[${i}]}"
  72.             p_name="$(ps -p "$pid" -o comm)"
  73.             if [[ $p_name == $name ]]; then
  74.                 unset -v pids[${i}]
  75.             fi
  76.         done
  77.     done
  78.  
  79.     kill -9 "${pids[@]}"
  80. }
  81.  
  82. # Creates a file name for the log.
  83. log_killed="${HOME}/firefox_chromium_killed.log"
  84.  
  85. # If $log_killed is not a file, create it.
  86. if [[ ! -f $log_killed ]]; then
  87.     touch "$log_killed"
  88. fi
  89.  
  90. # Creates an infinite while loop.
  91. while true; do
  92.  
  93.     # Sleeps for 1 second.
  94.     sleep 1
  95.  
  96.     # Unsets $free_ram, since this might not be the first time the loop
  97.     # is run.
  98.     unset -v free_ram
  99.  
  100.     # Runs 'free', stores output in the $free_ram array,
  101.     # and sets a couple of variables based on that output.
  102.     mapfile -t free_ram < <(free)
  103.     ram=$(echo "${free_ram[1]}" | sed 's/ \+/ /g' | cut -d' ' -f4)
  104.     swap=$(echo "${free_ram[2]}" | sed 's/ \+/ /g' | cut -d' ' -f4)
  105.     avail=$(echo "${free_ram[1]}" | sed 's/ \+/ /g' | cut -d' ' -f7)
  106.  
  107.     # Prints the free and available RAM and SWAP.
  108.     echo "FREE (kibibytes)"
  109.     echo "RAM: ${ram}, SWAP: ${swap}"
  110.     echo "***"
  111.     echo "AVAILABLE (kibibytes)"
  112.     echo -e "RAM: ${avail}\n"
  113.  
  114.     # If available RAM is less than 1GB...
  115.     if [[ $avail -lt 1000000 ]]; then
  116.  
  117.         # Checks if Firefox and Chromium are running.
  118.         # $is_chromium is an array, since the output probably spans
  119.         # across multiple lines, due to Chromium being highly
  120.         # multithreaded and keeping separate threads / processes
  121.         # for every window and tab.
  122.         is_firefox=$(pgrep -x firefox)
  123.         is_tor=$(pgrep -x tor)
  124.         mapfile -t is_chromium < <(is_chromium)
  125.  
  126.         # If Firefox is running, then kill it, print a message to the
  127.         # screen, and append a message to the log.
  128.         if [[ $is_firefox ]]; then
  129.  
  130.             time=$(now)
  131.  
  132.             echo -e "${time}: Killing Firefox...\n" | tee --append "$log_killed"
  133.             kill_firefox
  134.         fi
  135.  
  136.         # If Tor Browser is running, then kill it, print a message to
  137.         # the screen, and append a message to the log.
  138.         if [[ $is_tor ]]; then
  139.  
  140.             time=$(now)
  141.  
  142.             echo -e "${time}: Killing Tor Browser...\n" | tee --append "$log_killed"
  143.             kill_firefox
  144.         fi
  145.  
  146.         # If Chromium is running, then...
  147.         if [[ ! ${is_chromium[0]} =~ $regex ]]; then
  148.  
  149.             time=$(now)
  150.  
  151.             echo -e "${time}: Killing Chromium...\n" | tee --append "$log_killed"
  152.             kill_chromium
  153.         fi
  154.  
  155.         # Writes cached writes to disk. Hopefully this will also clear
  156.         # up a little RAM.
  157.         sync
  158.  
  159.     fi
  160.  
  161. done
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top