#!/bin/sh
# a daemon, start and monitor servers
# 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
export_env

INTERVAL=60
pidfile="${APPRUNS_PATH}/serverdaemon.pid"

if [ -f "$pidfile" ]; then
  pid=$(cat "$pidfile")
  kill -0 "$pid" 2>/dev/null && exit 0
  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

  if ! is_running "${APPBINS_PATH}/${APPNAME}"
  then
    echo "starting ${APPNAME}: ${APPBINS_PATH}/${APPNAME} &"
    rm -rf ${APPRUNS_PATH}/*.lock
    ${APPBINS_PATH}/${APPNAME} &
  fi

  sleep $INTERVAL
done
