From 5a6bec76248cdc6400cc6c7fdf5fcebf84ff57c4 Mon Sep 17 00:00:00 2001 From: grothedev Date: Mon, 17 Jun 2024 13:06:12 -0500 Subject: 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 --- cpu_limit_monitor.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ mostrecentrecursively.sh | 2 ++ 2 files changed, 44 insertions(+) create mode 100755 cpu_limit_monitor.sh create mode 100755 mostrecentrecursively.sh 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 -- cgit v1.2.3