-
Notifications
You must be signed in to change notification settings - Fork 1
/
pscheck.sh
executable file
·74 lines (65 loc) · 1.23 KB
/
pscheck.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
67
68
69
70
71
72
73
74
# Copyright (C) 2023 Sergey Vlasov <[email protected]>
# MIT License
if [ $# -lt 2 ]; then
BASENAME=$(basename $0)
echo "$BASENAME: too few arguments"
echo "usage: $BASENAME PID NAME..."
exit 2
fi
PID=$1; shift
NAMES=$@
process_name() {
case "$OSTYPE" in
"darwin"*)
ps -p $1 -o comm=
;;
*)
if [ -f /proc/$1/comm ]; then
cat /proc/$P/comm
fi
;;
esac
}
is_process_stopped() {
case "$OSTYPE" in
"darwin"*)
[ "$(ps -p $1 -o state=)" = "T" ]
;;
*)
[ "$(sed -n '/^State:/s/State:\t\(.\).*/\1/p' /proc/$1/status)" = "T" ]
;;
esac
}
process_children() {
case "$OSTYPE" in
"darwin"*)
pgrep -P $1 -a
;;
*)
cat /proc/$1/task/$1/children
;;
esac
}
walk() {
for P in $@; do
CMD_NAME="$(process_name $P)"
if [ -z "$CMD_NAME" ]; then # process no longer exists or something else
continue
fi
for N in $NAMES; do
if [ "$N" = "$CMD_NAME" ]; then # it's a match
if (is_process_stopped $P); then
exit 1
fi
echo "$N"
exit 0
fi
done
CHILDREN=$(process_children $P)
if [ ! -z "$CHILDREN" ]; then
walk $CHILDREN
fi
done
}
walk $PID
exit 1