| 1 | #!/bin/bash |
| 2 | # kill-process.sh |
| 3 | |
| 4 | NOPROCESS=2 |
| 5 | |
| 6 | process=xxxyyyzzz # Use nonexistent process. |
| 7 | # For demo purposes only... |
| 8 | # ... don't want to actually kill any actual process with this script. |
| 9 | # |
| 10 | # If, for example, you wanted to use this script to logoff the Internet, |
| 11 | # process=pppd |
| 12 | |
| 13 | t=`pidof $process` # Find pid (process id) of $process. |
| 14 | # The pid is needed by 'kill' (can't 'kill' by program name). |
| 15 | |
| 16 | if [ -z "$t" ] # If process not present, 'pidof' returns null. |
| 17 | then |
| 18 | echo "Process $process was not running." |
| 19 | echo "Nothing killed." |
| 20 | exit $NOPROCESS |
| 21 | fi |
| 22 | |
| 23 | kill $t # May need 'kill -9' for stubborn process. |
| 24 | |
| 25 | # Need a check here to see if process allowed itself to be killed. |
| 26 | # Perhaps another " t=`pidof $process` " or ... |
| 27 | |
| 28 | |
| 29 | # This entire script could be replaced by |
| 30 | # kill $(pidof -x process_name) |
| 31 | # or |
| 32 | # killall process_name |
| 33 | # but it would not be as instructive. |
| 34 | |
| 35 | exit 0 |