View Single Post
Posts: 602 | Thanked: 735 times | Joined on Mar 2011 @ Nantes, France
#596
Hi,
I've recently seen than when the sensors driver crashes, it may eats too much CPU (we all know that) but the process may also be shutdown for good. So, I've modified the watchdog script to restart the service if no sensor process is found. I'm far from being a bash expert, feel free to correct me if I did something wrong.

Code:
#!/bin/bash
 
# This checks every 1/2 minute and kills if
# sensors.qcom 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 sensors.qcom | 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 sensors.qcom: ${cpu_usage/.*}"
    if [[ ${cpu_usage/.*} -ge 8 ]]; then
            if [[ $kill_count -ge 1 ]]; then
                    echo "CPU Usage of sensors.qcom too high restarting..."
                    systemctl restart sensorfwd
                    kill_count=0
            else
                    echo "CPU Usage of sensors.qcom too high! Setting kill_count + 1"
                    kill_count=$((kill_count+1))
            fi
    else
            echo "Nothing to do"
    fi
  fi  
  sleep 30
done