-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkill_top_cpu_ps.sh
More file actions
executable file
·40 lines (31 loc) · 1.22 KB
/
Copy pathkill_top_cpu_ps.sh
File metadata and controls
executable file
·40 lines (31 loc) · 1.22 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
#!/bin/bash
#Takes highest processes by CPU usage and SIGSTOP-s the top one. Currently seems to maybe operate with a delay.
#Defining minimum threshold below which a process wont be killed. This is for safety so that spamming this script shouldn't easily kill important processes.
minimum_threshold=10
#For multicore CPUs.
my_number_of_threads=4
#Need to multiply it by the number of threads because CPU usage percentage is classically shown based on a single thread (so what to us intuitively is 100% is here actually 400%).
minimum_threshold=$(( $minimum_threshold * $my_number_of_threads ))
#Take top process's row:
line=`ps -eo pcpu,pid | sort -k1 -r | sed -n '2p'`
#DEBUG
echo line: $line
cpu_percent=`echo "$line" | awk -F' ' '{ print $1 }'`
#DEBUG
echo cpu_percent: $cpu_percent
pid=`echo "$line" | awk -F' ' '{ print $2 }'`
#DEBUG
echo pid: $pid
#DEBUG
echo minimum_threshold: $minimum_threshold
#If over the minimum threshold
if [ $(echo $minimum_threshold $cpu_percent | awk '{if ($1 < $2) print 1; else print 0}' ) -eq 1 ]; then
#DEBUG
echo "true"
#Stop the process and send debug message.
kill -SIGSTOP $pid && echo "success" #putting to sleep 19
#kill -SIGKILL $pid #killing 9
else
#DEBUG
echo "false"
fi