View Single Post
Posts: 602 | Thanked: 735 times | Joined on Mar 2011 @ Nantes, France
#597
This script is buggy. Just experienced a sensor crash. As a result:
  • sensors.qcom processes was still active and ate about 0% of CPU
  • sensorfwd process was not running

I tweaked the script to monitor sensorfwd instead of sensors.qcom. And I'll see how things are going.

Code:
#!/bin/bash
 
# This checks every 1/2 minute and kills if
# sensorfwd process is eating more than 8% CPU for 1 minute
 
TERM=linux
export TERM
 
kill_count=0
 
while true; do
  sensors_pid=$(ps aux | grep sensorfwd | grep -v grep | awk '{print $2}')
  
  # If the process has crashed it may not be running,
  # so we restart it
  if [ -z $sensors_pid ]; then
    systemctl restart sensorfwd
  else
    cpu_usage=$(top -b -p $sensors_pid -n1 | grep $sensors_pid | awk '{print $9}') 
    echo $cpu_usage
    echo "CPU Usage of sensorfwd: ${cpu_usage/.*}"
    if [[ ${cpu_usage/.*} -ge 8 ]]; then
            if [[ $kill_count -ge 1 ]]; then
                    echo "CPU Usage of sensorfwd too high restarting..."
                    systemctl restart sensorfwd
                    kill_count=0
            else
                    echo "CPU Usage of sensorfwd too high! Setting kill_count + 1"
                    kill_count=$((kill_count+1))
            fi
    else
            echo "Nothing to do"
    fi
  fi  
  sleep 30
done
 

The Following User Says Thank You to romu For This Useful Post: