proc-wdtd 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/bin/bash
  2. #
  3. # Process watchdog timer daemon: monitor the processes to ensure the applications are running
  4. #
  5. CONF_FILE="/etc/proc-wdtd.conf"
  6. # check if the configure file exists
  7. if [[ -f ${CONF_FILE} ]]; then
  8. echo "proc-wdtd: load configure file '${CONF_FILE}'"
  9. else
  10. echo "proc-wdtd: ERROR: can not find file '${CONF_FILE}'"
  11. exit 1
  12. fi
  13. # load configuration
  14. CONF=$(cat ${CONF_FILE} 2>/dev/null | sed 's/#.*//' | \
  15. sed 's/^[[:blank:]]*//' | sed 's/[[:blank:]]*$//' | sed '/^$/d' | \
  16. sed 's/[[:blank:]]*=[[:blank:]]*/=/')
  17. # get application name list
  18. APPS=$(echo "${CONF}" | awk -F'=' '{print$1}')
  19. sleep 60
  20. while true; do
  21. for app in ${APPS}; do
  22. # check native program
  23. pid=$(pidof ${app})
  24. if [[ "${pid}" != "" ]]; then
  25. echo "proc-wdtd: '${app}' is running (${pid})"
  26. continue
  27. fi
  28. # check python script
  29. pid=$(ps aux | grep python | grep -v grep | grep ${app} | awk '{print$1}')
  30. if [[ "${pid}" != "" ]]; then
  31. echo "proc-wdtd: '${app}' python script is running (${pid})"
  32. continue
  33. fi
  34. # the program is not running, execute the command to lunch it
  35. echo "proc-wdtd: '${app}' is not running"
  36. cmd=$(echo "${CONF}" | grep "^${app}=" | head -n 1 | \
  37. cut -d "=" -s -f2- | sed 's/^"//' | sed 's/"$//')
  38. echo "proc-wdtd: execute command '${cmd}'"
  39. ${cmd} &>/dev/null &
  40. done
  41. sleep 60
  42. done