View Single Post
Posts: 602 | Thanked: 735 times | Joined on Mar 2011 @ Nantes, France
#604
Small change in the watchdog script. I've seen that when the sensors framework crashes and the sensorfwd process is killed, I can't restart the service if I don't kill the sensors.qcom process.

So, I implemented this into the script.

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
    echo "sensorfwd not found"
    # Before restarting the service, we must first kill the sensors.qcom process
    sensors_qcom=$(ps aux | grep sensors.qcom | grep -v grep | awk '{print $2}')
    kill $sensors_qcom
    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
Now, remaining issue: the sensors framework can crash, but the process may remain alive while not eating the CPU, so we need another way to detec the crash.

Last edited by romu; 2016-04-04 at 09:47.