summaryrefslogtreecommitdiff
path: root/cpu_limit_monitor.sh
diff options
context:
space:
mode:
authorgrothedev <grothedev@gmail.com>2024-06-17 13:06:12 -0500
committergrothedev <grothedev@gmail.com>2024-06-17 13:43:05 -0500
commit5a6bec76248cdc6400cc6c7fdf5fcebf84ff57c4 (patch)
tree6bb11c1606e895706e2fc4d212170d272129d092 /cpu_limit_monitor.sh
parentfecda2be6e3238a35b9a0569b9e12c9c84faabf3 (diff)
a script ive been using to get the top most recently modified files in a folder recursively. i have another version of this script with the option for file exclusion and outputs how long it took to run. also a script i used to kill vscode-server processes that were destroying my laptop when using vscode ssh from desktop
Diffstat (limited to 'cpu_limit_monitor.sh')
-rwxr-xr-xcpu_limit_monitor.sh42
1 files changed, 42 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