-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkillAll
More file actions
executable file
·43 lines (36 loc) · 1.34 KB
/
killAll
File metadata and controls
executable file
·43 lines (36 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/bash
# Author: Hamdi Roumani
# Description: Similar to pkill / killall but will prompt before killing any
# process.
# -----------------------------------------------------------------------------
if [ "$#" -ne 1 ]; then
echo "Usage: killAll <process to kill name>"
exit
fi
# Capture all matching processes less grep and ps it self.
out=`ps -elf | grep -i "$1 " | grep -v "grep " | grep -v "ps "`
if [ -z "$out" ]; then
exit;
fi
while read -r line
do
# Capture process PID and description
pname=`echo $line | awk '{print substr($0, index($0,$15))}'`
pid=`echo $line | awk '{print $4}'`
# Prompt for action and act accordingly
# Note: The decision to look for a n/N instead of treating any input other
# than y/Y as a "do not kill" had to do with screen notifications, often
# when a process is killed (e.g. SSH) a bell is triggered by screen to
# notify and often enter is pressed to continue which would be interpreted
# as a "do not kill" decision.
while true; do
read -u 24 -p "Kill this process (\"$pname\", $pid) ? " answer
case $answer in
([yY]* ) kill $pid
break;;
([nN]* ) break;;
(* ) echo "Please use Y or N.";;
esac
done
# Need to redirect std out FD so we can use it to prompt within the while loop
done 24<&0 <<<"$out"