diff options
| -rwxr-xr-x | cpu_limit_monitor.sh | 42 | ||||
| -rwxr-xr-x | mostrecentrecursively.sh | 2 |
2 files changed, 44 insertions, 0 deletions
diff --git a/cpu_limit_monitor.sh b/cpu_limit_monitor.sh new file mode 100755 index 0000000..4b0bda2 --- /dev/null +++ b/cpu_limit_monitor.sh @@ -0,0 +1,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
\ No newline at end of file diff --git a/mostrecentrecursively.sh b/mostrecentrecursively.sh new file mode 100755 index 0000000..cc2b8bf --- /dev/null +++ b/mostrecentrecursively.sh @@ -0,0 +1,2 @@ +#!/bin/bash +find $1 -type f -exec stat --format '%Y :%y %n' "{}" \; | sort -nr | cut -d: -f2- | head |
