-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu_limit_monitor.sh
executable file
·42 lines (37 loc) · 1.36 KB
/
cpu_limit_monitor.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
#!/bin/bash
MAX_CPU_USAGE=30 #for limiting processes matching the query
MAX_CPU_USAGE2=60 #for reporting (and optionally limiting) any other process
# Specify the log file location
LOG_FILE="/var/log/cpulimit.log"
query='[v]scode-server'
if [[ $1 ]]; then
query=${1}
fi
while true; do
#get the processes of interest and see if they are using too much cpu
PIDS=$(ps aux | grep ${query} | awk '{print $2}')
if [ -z "$PIDS" ]; then
echo "[$(date)] No ${query} processes found." | tee -a $LOG_FILE
else
# Apply the CPU limit to each offending process
for PID in $PIDS
do
cpulimit -p $PID -l $MAX_CPU_USAGE &
echo "[$(date)] CPU limit of $MAX_CPU_USAGE% applied to process with PID: $PID" | tee -a $LOG_FILE
done
fi
#see if there are any other processes using too much cpu
#procs=$(ps ax -o pid,%cpu,%mem,args)
PIDS=$(ps aux | grep -v PID | awk '{print $2}')
for pid in ${PIDS}; do
res=$(ps -p $pid -o %cpu,args | grep -v %CPU)
cpuu=$(echo ${res} | awk '{print $1}')
args=$(echo ${res} | awk '{print $2}')
if [[ -z ${cpuu} ]]; then continue; fi
if (( $(echo "${cpuu} > ${MAX_CPU_USAGE2}" | bc -l) )); then
echo "cpuu of ${pid} is ${cpuu}"
fi
done
# Sleep for a specified amount of time before checking again
sleep 5
done