diff options
| author | grothedev <grothedev@gmail.com> | 2024-10-23 15:51:10 -0500 |
|---|---|---|
| committer | grothedev <grothedev@gmail.com> | 2024-10-23 15:51:10 -0500 |
| commit | 5d0af662668242611eab1f5de64fa410bb27e7ab (patch) | |
| tree | 62bd959392617a69bb49c2f5d5166b1af681f7d5 | |
| parent | 95bc7c7e59999d3dbd18b7782e551d4fc9b16cf7 (diff) | |
ignore processes that are less than 2seconds old
| -rwxr-xr-x | procwatch.sh | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/procwatch.sh b/procwatch.sh index 2f5717d..ff1aa56 100755 --- a/procwatch.sh +++ b/procwatch.sh @@ -11,7 +11,7 @@ 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,etimes --sort=-pcpu | grep -v "PID") # echo $processes # Loop through the processes @@ -19,15 +19,17 @@ while true; do # Extract the process information pid=$(echo "$process_info" | awk '{print $1}') name=$(echo "$process_info" | awk '{print $4}') + dur=$(echo "$process_info" | awk '{print $5}') 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 (( ${dur} < 2 )); then continue; fi if (( $(echo "$cpu_percent > $((cpu_threshold/4))" | bc -l) )) || (( mem_usage > $((mem_threshold/4)) )); then - echo "PROCESS! ${pid}:${name} -- ${cpu_percent}% ${mem_usage}MB" + echo "PROCESS! ${pid}:${name} -- ${cpu_percent}% ${mem_usage}MB ${dur}s" 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 - 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" + msg_warn="$(date) - Process $name (PID: $pid) has exceeded the threshold: etime: ${dur}, 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 |
