Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # To close a misbehaving program properly.
- #
- # by desgua (desgua@gmail.com)
- #
- # License GPL
- #
- # version 0.03 02/22/2013
- name=$(echo $0 | sed 's/.*\///')
- if [ $# -ne 1 ]; then
- echo "
- Usage: $name <app to be closed>
- Example: $name gedit
- Info for kill signals: http://pthree.org/2012/08/14/appropriate-use-of-kill-9-pid
- "
- exit 1
- fi
- app=$1
- function CLOSE {
- if [[ "$a" == "10" ]]; then
- DECISION
- fi
- kill -15 `pidof $app`
- sleep .5
- if [[ "$(pidof $app)" == "" ]]; then
- echo "The app $app has been closed successfully."
- exit 0
- else
- a=$(echo $a+1 | bc)
- CLOSE
- fi
- }
- function DECISION {
- clear
- read -n 1 -p "I have tried to close the app $app for $a times now. What to do?
- [1] Keep trying more $a times.
- [2] Kill.
- [3] Exit leaving it open.
- " b
- if [[ "$b" == "1" ]]; then
- a=0
- CLOSE
- fi
- if [[ "$b" == "2" ]]; then
- KILL
- fi
- if [[ "$b" == "3" ]]; then
- echo
- echo
- echo "Goodbye then."
- echo
- exit 1
- fi
- DECISION
- }
- function KILL {
- clear
- 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."
- kill -1 `pidof $app`
- sleep .5
- if [[ "$(pidof $app)" == "" ]]; then
- echo "The app $app has been closed with SIGHUP (kill -1)."
- exit 0
- fi
- echo; echo
- 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."
- kill -2 `pidof $app`
- sleep .5
- if [[ "$(pidof $app)" == "" ]]; then
- echo "The app $app has been closed with SIGHUP SIGINT (kill -2)."
- exit 0
- fi
- echo; echo
- 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."
- kill -11 `pidof $app`
- sleep .5
- if [[ "$(pidof $app)" == "" ]]; then
- echo "The app $app has been closed with SIGSEGV (kill -11)."
- exit 0
- fi
- echo; echo
- 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..."
- kill -9 `pidof $app`
- sleep .5
- if [[ "$(pidof $app)" == "" ]]; then
- echo "The app $app has been closed with SIGKILL (kill -9)."
- exit 0
- fi
- clear
- echo "I am so sorry nothing has worked."
- sleep 2
- a=$(echo -n "many times and with many differents signals more then 10")
- DECISION
- }
- if [[ "$(pidof $app)" == "" ]]; then
- echo "The app $app is not running."
- exit 1
- else
- a=0
- CLOSE
- fi
- exit 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement