niujiuru 1 месяц назад
Родитель
Сommit
4fd2dc9a22
7 измененных файлов с 37 добавлено и 29 удалено
  1. 2 2
      scripts/modules/funcs/functions.sh
  2. 1 0
      scripts/modules/myapp
  3. 1 0
      scripts/modules/yfkj_sshd
  4. 21 16
      scripts/serverdaemon
  5. 1 1
      scripts/set_env
  6. 2 2
      scripts/start
  7. 9 8
      scripts/stop

+ 2 - 2
scripts/modules/funcs/functions.sh

@@ -60,7 +60,7 @@ is_running()
 
   for pid in /proc/[0-9]*; do
     exe=$(readlink "$pid/exe" 2>/dev/null) || continue
-    exe="${exe% *}"
+    exe="${exe% (deleted)}"
     [ "$exe" = "$target" ] && return 0
   done
 
@@ -84,7 +84,7 @@ kill_by_path()
 
   for pid in /proc/[0-9]*; do
     exe=$(readlink "$pid/exe" 2>/dev/null) || continue
-    exe="${exe% *}"
+    exe="${exe% (deleted)}"
     if [ "$exe" = "$target" ]; then
       kill -$sig "${pid##*/}" 2>/dev/null
     fi

+ 1 - 0
scripts/modules/myapp

@@ -12,4 +12,5 @@ start()
 stop()
 {
   graceful_kill ${APPBINS_PATH}/${APPNAME}
+  rm -f ${APPRUNS_PATH}/${APPNAME}.lock
 }

+ 1 - 0
scripts/modules/yfkj_sshd

@@ -11,4 +11,5 @@ start()
 stop()
 {
   graceful_kill ${APPBINS_PATH}/${YFKJ_SSHD}
+  rm -f ${APPRUNS_PATH}/${YFKJ_SSHD}.lock
 }

+ 21 - 16
scripts/serverdaemon

@@ -3,37 +3,42 @@
 # 2013-09-27 09:28 created by niujiuru
 # 2026-01-05 13:40 updated by niujiuru
 
-. $(dirname $0)/set_env
-. $(dirname $0)/modules/funcs/functions.sh
+. "$(dirname "$0")/set_env"
+. "$(dirname "$0")/modules/funcs/functions.sh"
 export_env
 
 INTERVAL=60
 pidfile="${APPRUNS_PATH}/serverdaemon.pid"
 
 if [ -f "$pidfile" ]; then
-  pid=$(cat "$pidfile")
-  kill -0 "$pid" 2>/dev/null && exit 0
+  pid=$(cat "$pidfile" 2>/dev/null)
+  if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
+    exit 0
+  fi
   rm -f "$pidfile"
 fi
 
 mkdir -p "$(dirname "$pidfile")"
 echo $$ > "$pidfile"
 
-while true
-do
-  if ! is_running "${APPBINS_PATH}/${YFKJ_SSHD}"
-  then
-    echo "starting ${YFKJ_SSHD}: ${APPBINS_PATH}/${YFKJ_SSHD} &"
-    rm -rf ${APPRUNS_PATH}/*.lock
-    ${APPBINS_PATH}/${YFKJ_SSHD} &
-  fi
+start_if_needed()
+{
+  name="$1"
+  path="$2"
+  lock="${APPRUNS_PATH}/${name}.lock"
 
-  if ! is_running "${APPBINS_PATH}/${APPNAME}"
+  if ! is_running "$path"
   then
-    echo "starting ${APPNAME}: ${APPBINS_PATH}/${APPNAME} &"
-    rm -rf ${APPRUNS_PATH}/*.lock
-    ${APPBINS_PATH}/${APPNAME} &
+    rm -f "$lock"
+    echo "starting $name: $path"
+    "$path" &
   fi
+}
+
+while true
+do
+  start_if_needed "${YFKJ_SSHD}" "${APPBINS_PATH}/${YFKJ_SSHD}"
+  start_if_needed "${APPNAME}"   "${APPBINS_PATH}/${APPNAME}"
 
   sleep $INTERVAL
 done

+ 1 - 1
scripts/set_env

@@ -3,7 +3,7 @@ export APPBINS_PATH="${APPHOME_PATH}"
 export APPLIBS_PATH="${APPHOME_PATH}/lib"
 export APPRUNS_PATH="${APPHOME_PATH}/status"
 export APPNAME="rtu_linux_modules.out"
-export YFKJ_SSHD="yfkj_sshd.out" # 远程运维服务
+export YFKJ_SSHD="yfkj_sshd.out" #远程运维服务
 
 #导出运行时库
 export LD_LIBRARY_PATH=${APPLIBS_PATH}:${APPLIBS_PATH}/GenICam/bin

+ 2 - 2
scripts/start

@@ -2,8 +2,8 @@
 # script that runs on system startup
 # 2013-09-26 09:30 created by niujiuru
 
-. $(dirname $0)/set_env
-. $(dirname $0)/modules/funcs/functions.sh
+. "$(dirname "$0")/set_env"
+. "$(dirname "$0")/modules/funcs/functions.sh"
 export_env
 
 # 启动主应用程序模块

+ 9 - 8
scripts/stop

@@ -2,8 +2,8 @@
 # script that runs on system stop
 # 2013-09-26 09:50 created by niujiuru
 
-. $(dirname $0)/set_env
-. $(dirname $0)/modules/funcs/functions.sh
+. "$(dirname "$0")/set_env"
+. "$(dirname "$0")/modules/funcs/functions.sh"
 export_env
 
 ORDER="
@@ -12,13 +12,14 @@ myapp
 yfkj_sshd
 "
 
-for m in ${ORDER}
+if [ "$#" -gt 0 ]; then
+  APPS="$*"
+else
+  APPS="$ORDER"
+fi
+
+for m in ${APPS}
 do
   echo ":: stopping $m"
   call_module "$m" stop
 done
-
-if [ -d "${APPRUNS_PATH}" ]; then
-  echo ":: removing all files from ${APPRUNS_PATH}"
-  rm -f ${APPRUNS_PATH}/* 2>/dev/null
-fi