/
killrenegade.sh
executable file
·164 lines (126 loc) · 4.52 KB
/
killrenegade.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
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
164
#!/bin/bash
# killrenegade version 1.2 august 2008 written by Feherke
# kill the processes with given name which use to much resources
shopt -s extglob
signal=( '' 'SIGHUP' 'SIGINT' 'SIGQUIT' 'SIGILL' 'SIGTRAP' 'SIGABRT' 'SIGBUS' 'SIGFPE' 'SIGKILL' 'SIGUSR1' 'SIGSEGV' 'SIGUSR2' 'SIGPIPE' 'SIGALRM' 'SIGTERM' 'SIGSTKFLT' 'SIGCHLD' 'SIGCONT' 'SIGSTOP' 'SIGTSTP' 'SIGTTIN' 'SIGTTOU' 'SIGURG' 'SIGXCPU' 'SIGXFSZ' 'SIGVTALRM' 'SIGPROF' 'SIGWINCH' 'SIGIO' 'SIGPWR' 'SIGSYS' )
sect=( '' ); comm=(); user=(); uown=(); mcpu=(); mmem=(); sign=(); inte=()
sec=''
nr=0
verb=0
echo 'killrenegade version 1.2 august 2008 written by Feherke'
while (( $# )); do
case "$1" in
'-i') shift; inifile="$1" ;;
'--ini='*) inifile="${1#*=}" ;;
'-V'|'--verbose') (( verb++ )) ;;
'-v'|'--version') exit ;;
'-h'|'--help'|'-?')
cat <<ENDOFSYNTAX
kill the processes with given name which use to much resources
Syntax :
killrenegade.sh [-i inifile] [-V]
Parameters :
-i inifile | --ini=inifile - settings file to be read ( killrenegade.ini )
-V | --verbose - verbose mode, use twice for more verbose ( no )
ENDOFSYNTAX
exit
;;
esac
shift
done
selfdir="${0%/*}"
: "${inifile=${0%.sh}.ini}"
[[ -f "$inifile" ]] || { echo 'killrenegade: error no settings'; exit 1; }
echo -n 'reading settings... '
while read str; do
[[ "$str" ]] || continue
[[ "${str:0:1}" == '#' ]] && continue
[[ "$str" == \[*\] ]] && {
sec="${str:1:$(( ${#str}-2 ))}"
[[ "$sec" == 'default' ]] && nr=0 || { [[ "$sec" != 'general' ]] && { nr=${#sect[@]}; sect[nr]="$sec"; } }
continue
}
[[ "$sec" ]] || continue
[[ "${str#?*=}" != "$str" ]] && {
IFS='=' read -r key val <<< "$str"
if [[ "$sec" == 'general' ]]; then
eval "$key=$val"
else
case "$key" in
'command') comm[nr]="$val" ;;
'user') user[nr]="$val" ;;
'belong') uown[nr]="$val" ;;
'processor') mcpu[nr]="$val" ;;
'memory') mmem[nr]="$val" ;;
'signal') sign[nr]="$val" ;;
'interval') inte[nr]="$val" ;;
esac
fi
}
done < "$inifile"
(( nrsec=${#sect[@]}-1 ))
echo "ok, $nrsec sections found"
echo -n 'verifying settings... '
err=0
for ((i=1;i<=nrsec;i++)); do
comm[i]="${comm[i]:-comm[0]}"
user[i]="${user[i]:-${user[0]:-root}}"
uown[i]="${uown[i]:-${uown[0]:-no}}"
mcpu[i]="${mcpu[i]:-${mcpu[0]:-10}}"
mmem[i]="${mmem[i]:-${mmem[0]:-25}}"
sign[i]="${sign[i]:-${sign[0]:-SIGTERM}}"
inte[i]="${inte[i]:-${inte[0]:-60}}"
last[i]=0
[[ "${sign[$i]}" == @([[:alpha:]])* ]] || sign[i]="${signal[sign[i]]}"
[[ "${comm[i]}" ]] || (( err++ ))
done
(( err )) && { echo "killrenegade: $err error(s) in settings"; exit 2; }
last[0]=$( date +'%s' )
[[ "$logfile" && ! -e "$logfile" ]] && touch "$logfile"
[[ "$logfile" && -x "$selfdir/killstatistic.awk" ]] || htmldir=''
[[ "$htmldir" && ! -e "$htmldir" ]] && mkdir -p "$htmldir"
echo 'ok, defaults used where needed'
[[ "$USER" != 'root' ]] && {
echo "running as $USER, restrictions applied"
for ((i=1;i<=nrsec;i++)); do
user[i]="$USER"
uown[i]='yes'
done
}
for ((i=0;i<=nrsec;i++)); do echo -e "$i\t${sect[i]}\t${comm[i]}\t${user[i]}\t${uown[i]}\t${mcpu[i]}\t${mmem[i]}\t${sign[i]}\t${inte[i]}"; done
(( verb )) && echo 'verbose mode on, will announce kills'
echo "started on $( date ), Ctrl-C to stop..."
while :; do
(( verb>1 )) && date
now=$( date +'%s' )
for ((i=1;i<=nrsec;i++)); do
if (( last[i]+inte[i]<=now )); then
(( verb>1 )) && echo " - ${sect[i]}"
ps h -C "${comm[i]}" -o 'pid user=twentycharacterswide %cpu %mem' | \
while read pid user cpu mem; do
[[ "${user[i]}" != '-' ]] && { grep -q -w $( sed -n '/no/i-v' <<< "${uown[i]}" ) "$user" <<< "${user[i]}" || continue; }
cpui="${cpu%.*}"
memi="${mem%.*}"
(( cpui>mcpu[i] || memi>mmem[i] )) && {
(( verb )) && echo -n "kill ${comm[i]} ( $pid ) "
for sig in ${sign[i]}; do
(( verb )) && echo -n "$sig... "
kill -"$sig" "$pid" && break
done
(( verb )) && echo 'done'
[[ "$logfile" ]] && echo -e "$user\t$( date +'%Y-%m-%d %H:%M:%S' )\t$cpu\t$mem\t${comm[$i]}\t$sig" >> "$logfile"
}
done
last[i]=$now
fi
done
[[ "$htmldir" ]] && (( last[0]+build<=now )) && {
(( verb )) && echo 'build'
"$selfdir/killstatistic.awk" -v htmldir="$htmldir" "$logfile" &
last[0]=$now
}
next=0
for ((i=1;i<=nrsec;i++)); do temp=$(( last[i]+inte[i] )); (( next==0 || next>temp )) && next=$temp; done
now=$( date +'%s' )
(( next>now )) && sleep $(( next-now ))
done