Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

close-app

By: desgua on Feb 21st, 2013  |  syntax: Bash  |  size: 2.99 KB  |  views: 74  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/bin/bash
  2.  
  3. # To close a misbehaving program properly.
  4. #
  5. # by desgua (desgua@gmail.com)
  6. #
  7. # License GPL
  8. #
  9. # version 0.03 02/22/2013
  10.  
  11. name=$(echo $0 | sed 's/.*\///')
  12. if [ $# -ne 1 ]; then
  13.  
  14. echo "
  15. Usage: $name <app to be closed>
  16.  
  17. Example: $name gedit
  18.  
  19. Info for kill signals: http://pthree.org/2012/08/14/appropriate-use-of-kill-9-pid
  20. "
  21.  
  22. exit 1
  23. fi
  24.  
  25. app=$1
  26.  
  27. function CLOSE {
  28. if [[ "$a" == "10" ]]; then
  29.     DECISION
  30. fi
  31.  
  32. kill -15 `pidof $app`
  33.  
  34. sleep .5
  35.  
  36. if [[ "$(pidof $app)" == "" ]]; then
  37.     echo "The app $app has been closed successfully."
  38.     exit 0
  39. else
  40.     a=$(echo $a+1 | bc)
  41.     CLOSE
  42. fi
  43. }
  44.  
  45. function DECISION {
  46. clear
  47. read -n 1 -p "I have tried to close the app $app for $a times now. What to do?
  48.    [1] Keep trying more $a times.
  49.    [2] Kill.
  50.    [3] Exit leaving it open.
  51.    " b
  52.  
  53.     if [[ "$b" == "1" ]]; then
  54.         a=0
  55.         CLOSE
  56.     fi
  57.    
  58.     if [[ "$b" == "2" ]]; then
  59.         KILL
  60.     fi
  61.  
  62.     if [[ "$b" == "3" ]]; then
  63.         echo
  64.         echo
  65.         echo "Goodbye then."
  66.         echo
  67.         exit 1
  68.     fi
  69.  
  70. DECISION
  71. }
  72.  
  73. function KILL {
  74. clear
  75. echo "Trying SIGHUP (kill -1): flush any data to disk that needs to be written, and cleans up the memory registers, then restarts the program."
  76.  
  77. kill -1 `pidof $app`
  78.  
  79. sleep .5
  80.  
  81. if [[ "$(pidof $app)" == "" ]]; then
  82.     echo "The app $app has been closed with SIGHUP (kill -1)."
  83.     exit 0
  84. fi
  85.  
  86. echo; echo
  87.  
  88. echo "Trying SIGINT (kill -2). This is an interrupt from the keyboard signal. This is equivalent to sending a CTRL-C to the PID. This is useful when the PID is not in the terminal foreground, but has been backgrounded as a daemon process."
  89.  
  90. kill -2 `pidof $app`
  91.  
  92. sleep .5
  93.  
  94. if [[ "$(pidof $app)" == "" ]]; then
  95.     echo "The app $app has been closed with SIGHUP SIGINT (kill -2)."
  96.     exit 0
  97. fi
  98.  
  99. echo; echo
  100.  
  101. echo "Trying SIGSEGV (kill -11). This causes the program to experience a segmentation fault, and close the PID. It won’t flush any data that needs to be written to disk, but it may create a core dump file that could be useful in debugging, on learning why the program was behaving the way it was. The logging facility for that core dump is through the kernel."
  102.  
  103. kill -11 `pidof $app`
  104.  
  105. sleep .5
  106.  
  107. if [[ "$(pidof $app)" == "" ]]; then
  108.     echo "The app $app has been closed with SIGSEGV (kill -11)."
  109.     exit 0
  110. fi
  111.  
  112. echo; echo
  113.  
  114. echo "Trying SIGKILL (kill -9). It will not sync any data to disk. No unwritten data, no debugging data, no logging data, no nothing. It’s equivalent to using a sledge hammer to sink a nail..."
  115.  
  116. kill -9 `pidof $app`
  117.  
  118. sleep .5
  119.  
  120. if [[ "$(pidof $app)" == "" ]]; then
  121.     echo "The app $app has been closed with SIGKILL (kill -9)."
  122.     exit 0
  123. fi
  124.  
  125. clear
  126.  
  127. echo "I am so sorry nothing has worked."
  128. sleep 2
  129. a=$(echo -n "many times and with many differents signals more then 10")
  130. DECISION
  131. }
  132.  
  133. if [[ "$(pidof $app)" == "" ]]; then
  134.     echo "The app $app is not running."
  135.     exit 1
  136. else
  137.     a=0
  138.     CLOSE
  139. fi
  140.  
  141. exit 2