-
Notifications
You must be signed in to change notification settings - Fork 580
/
052-killall.sh
executable file
·66 lines (56 loc) · 1.74 KB
/
052-killall.sh
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/sh
# killall - send the specified signal to all processes that match a
# specific process name
# By default it only kills processes owned by the same user, unless
# you're root. Use -s SIGNAL to specify a signal to send, -u user to
# specify user, -t tty to specify a tty, and -n to only show what'd
# be done rather than doing it
signal="-INT" # default signal
user="" tty="" donothing=0
while getopts "s:u:t:n" opt; do
case "$opt" in
# note the trick below: kill wants -SIGNAL but we're asking
# for SIGNAL, so we slip the '-' in as part of the assignment
s ) signal="-$OPTARG"; ;;
u ) if [ ! -z "$tty" ] ; then
echo "$0: error: -u and -t are mutually exclusive." >&2
exit 1
fi
user=$OPTARG; ;;
t ) if [ ! -z "$user" ] ; then
echo "$0: error: -u and -t are mutually exclusive." >&2
exit 1
fi
tty=$2; ;;
n ) donothing=1; ;;
? ) echo "Usage: $0 [-s signal] [-u user|-t tty] [-n] pattern" >&2
exit 1
esac
done
shift $(( $OPTIND - 1 ))
if [ $# -eq 0 ] ; then
echo "Usage: $0 [-s signal] [-u user|-t tty] [-n] pattern" >&2
exit 1
fi
if [ ! -z "$tty" ] ; then
pids=$(ps cu -t $tty | awk "/ $1$/ { print \$2 }")
elif [ ! -z "$user" ] ; then
pids=$(ps cu -U $user | awk "/ $1$/ { print \$2 }")
else
pids=$(ps cu -U ${USER:-LOGNAME} | awk "/ $1$/ { print \$2 }")
fi
if [ -z "$pids" ] ; then
echo "$0: no processes match pattern $1" >&2; exit 1
fi
for pid in $pids
do
# Sending signal $signal to process id $pid: kill might
# still complain if the process has finished, user doesn't
# have permission, etc, but that's okay.
if [ $donothing -eq 1 ] ; then
echo "kill $signal $pid"
else
kill $signal $pid
fi
done
exit 0