forked from astera/cupcakes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
killerapp
executable file
·42 lines (35 loc) · 1.16 KB
/
killerapp
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
#!/bin/bash
while true
do
COMMAND=`ps -Aco pid,pcpu,time,comm | sort -nrk2 | head -1`
PID=`echo $COMMAND | awk '{print $1}'`
PCPU=`echo $COMMAND | awk '{print $2}'`
PCPU=${PCPU%%.*}
PTIME=`echo $COMMAND | awk '{print $3}'`
PTIME=${PTIME%%:*}
PCMD=`echo $COMMAND | awk '{print $4}'`
MAXUSE=80
MAXHOURS=1
if [ $PCPU -ge $MAXUSE ] && [ $PTIME -ge $MAXHOURS ];
then
sleep 20
NEWCOMMAND=`ps -Aco pid,pcpu,time,comm | sort -nrk2 | head -10 | grep $PID`
NEWPCPU=`echo $NEWCOMMAND | awk '{print $2}'`
NEWPCPU=${NEWPCPU%%.*}
NEWTIME=`echo $NEWCOMMAND | awk '{print $3}'`
NEWTIME=${NEWTIME%%:*}
if [ $NEWPCPU -ge $MAXUSE ] && [ $NEWTIME -ge $MAXHOURS ];
then
kill -15 $PID
sleep 20
if [ `ps -Aco pid,pcpu | sort -nrk2 | head -10 | grep -c $PID` == 1 ];
then
kill -9 $PID
echo `date`" - SIGKILLed $PCMD with PID $PID at $NEWPCPU CPU usage and $NEWTIME CPU time... na holleri!" >> killerlog.txt
else
echo `date`" - SIGTERMinated $PCMD with PID $PID at $NEWPCPU CPU usage and $NEWTIME CPU time" >> killerlog.txt
fi
fi
fi
sleep 60
done