Browse Source

上傳檔案到 ''

fatwolf 4 years ago
parent
commit
63d16faf47
1 changed files with 47 additions and 0 deletions
  1. 47 0
      proc-wdtd

+ 47 - 0
proc-wdtd

@@ -0,0 +1,47 @@
+#!/bin/bash
+#
+# Process watchdog timer daemon: monitor the processes to ensure the applications are running
+#
+
+CONF_FILE="/etc/proc-wdtd.conf"
+
+# check if the configure file exists
+if [[ -f ${CONF_FILE} ]]; then
+	echo "proc-wdtd: load configure file '${CONF_FILE}'"
+else
+	echo "proc-wdtd: ERROR: can not find file '${CONF_FILE}'"
+	exit 1
+fi
+
+# load configuration
+CONF=$(cat ${CONF_FILE} 2>/dev/null | sed 's/#.*//' | \
+		sed 's/^[[:blank:]]*//' | sed 's/[[:blank:]]*$//' | sed '/^$/d' | \
+		sed 's/[[:blank:]]*=[[:blank:]]*/=/')
+
+# get application name list
+APPS=$(echo "${CONF}" | awk -F'=' '{print$1}')
+
+sleep 60
+while true; do
+	for app in ${APPS}; do
+		# check native program
+		pid=$(pidof ${app})
+		if [[ "${pid}" != "" ]]; then
+			echo "proc-wdtd: '${app}' is running (${pid})"
+			continue
+		fi
+		# check python script
+		pid=$(ps aux | grep python | grep -v grep | grep ${app} | awk '{print$1}')
+		if [[ "${pid}" != "" ]]; then
+			echo "proc-wdtd: '${app}' python script is running (${pid})"
+			continue
+		fi
+		# the program is not running, execute the command to lunch it
+		echo "proc-wdtd: '${app}' is not running"
+		cmd=$(echo "${CONF}" | grep "^${app}=" | head -n 1 | \
+				cut -d "=" -s -f2- | sed 's/^"//' | sed 's/"$//')
+		echo "proc-wdtd: execute command '${cmd}'"
+		${cmd} &>/dev/null &
+	done
+	sleep 60
+done