diff options
| author | grothedev <grothedev@gmail.com> | 2024-10-23 12:03:02 -0500 |
|---|---|---|
| committer | grothedev <grothedev@gmail.com> | 2024-10-23 12:03:02 -0500 |
| commit | 95bc7c7e59999d3dbd18b7782e551d4fc9b16cf7 (patch) | |
| tree | 272ff2c2a303463971d9c5f9c7e3bc6ca2f458b0 /procwatch.sh | |
| parent | f70b6c9bfe22f01e89fe328f80dd0254a9116b31 (diff) | |
update process monitor script. kill processes that are using a ton of resources and just output processes that are using 1/4th of the threshold
Diffstat (limited to 'procwatch.sh')
| -rwxr-xr-x | procwatch.sh | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/procwatch.sh b/procwatch.sh index 0088b3f..2f5717d 100755 --- a/procwatch.sh +++ b/procwatch.sh @@ -4,28 +4,50 @@ log_file="process_monitor.log" # Set the CPU and memory usage thresholds -cpu_threshold=50 # Percentage -mem_threshold=500 # Megabytes +cpu_threshold=396 # Percentage +mem_threshold=15360 # Megabytes +offender=-1 +kill_attempts=0 while true; do # Get the list of running processes - processes=$(ps -eo pid,pcpu,rss,comm --sort=-pcpu | grep -v "^ PID") + processes=$(ps -eo pid,pcpu,rss,comm --sort=-pcpu | grep -v "PID") # echo $processes # Loop through the processes while read -r process_info; do - echo "PROCESS! "${process_info} - echo "" # Extract the process information pid=$(echo "$process_info" | awk '{print $1}') name=$(echo "$process_info" | awk '{print $4}') cpu_percent=$(echo "$process_info" | awk '{print $2}') mem_usage=$(echo "$process_info" | awk '{print $3}') mem_usage=$((mem_usage / 1024)) # Convert to Megabytes - + if (( $(echo "$cpu_percent > $((cpu_threshold/4))" | bc -l) )) || (( mem_usage > $((mem_threshold/4)) )); then + echo "PROCESS! ${pid}:${name} -- ${cpu_percent}% ${mem_usage}MB" + fi # Check if the process exceeds the CPU or memory usage threshold if (( $(echo "$cpu_percent > $cpu_threshold" | bc -l) )) || (( mem_usage > mem_threshold )); then - echo "$(date) - Process '$name' (PID: $pid) has exceeded the threshold: CPU usage: ${cpu_percent}%, Memory usage: ${mem_usage} MB" # >> "$log_file" + msg_warn="$(date) - Process $name (PID: $pid) has exceeded the threshold: CPU usage: ${cpu_percent}%, Memory usage: ${mem_usage} MB. Killing in 10 seconds" # >> "$log_file" + echo ${msg_warn} + notify-send -t 8000 "$msg_warn" + sleep 10 + if [[ $pid == $offender ]]; then + kill_attempts=$((kill_attempts+1)) + else + offender=${pid} + #TODO deal with multiple offending processes later + fi + if [[ ${kill_attempts} -gt 3 ]]; then + echo "force kill ${pid}" + #kill -9 ${pid} + else + echo "kill ${pid}" + #kill ${pid} + fi + if [[ -z $(pgrep -f ${pid}) ]]; then + offender=-1 + kill_attempts=0 + fi fi done <<< "$processes" |
