-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpstop
More file actions
executable file
·163 lines (141 loc) · 4.17 KB
/
pstop
File metadata and controls
executable file
·163 lines (141 loc) · 4.17 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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env bash
# ps | awk '{printf "%s\t", $1;$1=$2=$3=$4="";gsub(/^[ ]+/,"");printf "\047%s\047\n", $0}'
# Process stopper script
# USAGE: pstop <process name> <options>
#
# OPTIONS:
# -l List all processes called <process name> and their PID
# -L List all processes and their PID
# -s send SIGHUP to end hung up processes
# -h show this information
# -f kill all processes called <process name> immediately
# -k Send KILL-signal
#
grepstr=$1
#declare -a cmd_arg_array
echo "arguments are: ${@:2}"
cmd_args_raw="$(echo ${@:2} | tr -c -d [klshf])"
#echo $cmd_args_raw
#exit 0
cmd_args="$(echo $cmd_args_raw | grep -o . | sort -u | tr -d '\n')"
#echo $cmd_args_raw
#echo $cmd_args
#if [[ $cmd_args_raw =~ "
shopt -s extglob
helpfx(){
echo "Process stopper script"
echo "USAGE: pstop <process name> <options>"
echo "OPTIONS:"
echo " -l List all processes called <process name> and their PIDs"
echo " -L List all processes and their PIDs (Does not require <proces name>, cannot be combined with other options"
echo " -s send SIGHUP to end hung up processes, cannot be combined with '-k'"
echo " -f terminate all processes called <process name> immediately"
echo " -k kill selected process with signal 9"
}
getproc(){
# ps -Af | grep -i [$1] | grep -v -e "grep" -e "$$" -e "$BASHPID" | awk -v awkvar="$BASHPID" -v awkvar2="$$" '{printf "%s %s %s %s\n", $1, $5, awkvar, awkvar2}'
declare -A proclist
while IFS=" " read -r proc_pid proc_name; do # Populate this array with the results from a search for <process name> within all processes
proclist["$proc_pid"]=$proc_name
# echo "within while loop"
# echo "PID: $proc_pid"
# echo "CMD: $proc_name"
done < <(ps -Af | grep -i "$grepstr" | grep -v -e "pstop" -e "grep" -e "$$" -e "$BASHPID" | awk '{printf "%s\t", $2;$1=$2=$3=$4=$5=$6=$7="";gsub(/^[ ]+/,"");printf "\047%s\047\n", $0}' | awk -F'\t' '{print $1, $2}')
# done < <(ps -Af | grep -i "$grepstr" | grep -v -e "grep" -e "$$" -e "$BASHPID" | awk -v awkvar="$BASHPID" -v awkvar2="$$" '{printf "%s %s %s %s\n", $1, $5, awkvar, awkvar2}')
pid_cmd_sbst="$!"
# echo "last PID: $pid_cmd_sbst"
unset proclist[$pid_cmd_sbst]
declare -A procno # create an assoc. array in which to store the previously identified PIDs and a numeric ID
counter=1
echo -e "No.\tPID\tNAME\t\tFULL COMMAND"
while IFS=" " read -r number pid name; do # Populate this array by reading through the previous array and assign a number to each PID
procno["$number"]="$pid"
cmd=$(echo $name | awk -F' ' '{gsub(/\047/,"");print $1}' | xargs -n1 basename)
echo -e "${number}.\t$pid\t$cmd\t\t$name"
done < <(for pid in "${!proclist[@]}"; do
echo "$counter $pid ${proclist[$pid]}"
counter=$((counter+1))
done)
# echo "This is what is stored in the array:"
# for nmb in "${!procno[@]}"; do
# echo "$nmb ${procno[$nmb]}"
# done
case $cmd_args in
*f*) killchoice="all"
;;
*l*) exit 0
;;
*) echo "Which processes would you like to kill?"
echo "[enter number (1 - ${#procno[@]}) or multiple seperated by ',']"
echo "[enter 'all' to kill them all]"
echo "[enter 'q' to quit]"
read killchoice
;;
esac
case $killchoice in
all)
local killargs=""
for pid in "${procno[@]}"; do
killargs="$killargs $pid"
done
;;
q)
exit 0
;;
*)
local killargs=""
declare -a killnumbarray
while IFS=' ' read -r killnumbs; do
# echo "entered $killnumbs"
killnumbarray+=( $killnumbs )
done < <(echo $killchoice | tr ',' '\n')
for numb in ${killnumbarray[@]}; do
killargs="$killargs ${procno[$numb]}"
# echo "List of PIDs to kill is now: $killargs"
done
;;
esac
killproc $killargs
}
killproc(){
case $cmd_args in
*k*)
local signal_no="9"
local signal_name="SIGKILL"
;;
*s*)
local signal_no="1"
local signal_name="SIGHUP"
;;
*)
local signal_no="15"
local signal_name="SIGTERM"
;;
esac
echo "killing the following PIDS: $@"
for i in $@; do
kill -n "$signal_no" $i
echo "$signal_name send to PID $i"
done
}
#compute_args(){
#
# case $cmd_args in
# s)
#}
case $grepstr in
*L*)
ps -Af
exit 0
;;
*help*)
helpfx
exit 0
;;
*)
getproc
;;
esac
#echo $BASHPID
#echo $$
exit 0