github.com/klaytn/klaytn@v1.12.1/build/rpm/etc/init.d/kcnd (about)

     1  #!/bin/bash
     2  #
     3  # kcnd         Startup script for the kcn
     4  #
     5  # chkconfig: - 85 15
     6  # description : kcnd is Klaytn consensus node daemon
     7  #
     8  # processname: kcnd
     9  # config:  /etc/kcnd/conf/kcnd.conf
    10  # pidfile: /var/run/kcnd.pid
    11  #
    12  
    13  # Source function library.
    14  . /etc/init.d/functions
    15  
    16  if [ -f /etc/kcnd/conf/kcnd.conf ]; then
    17          . /etc/kcnd/conf/kcnd.conf
    18  fi
    19  
    20  kcn=/usr/bin/kcn
    21  prog=kcnd
    22  lockfile=${LOCKFILE-/var/lock/subsys/kcnd}
    23  pidfile=${PIDFILE-/var/run/kcnd.pid}
    24  auto_restart_daemon_pidfile=/var/run/restart_daemon_kcnd.pid
    25  RETVAL=0
    26  STOP_TIMEOUT=${STOP_TIMEOUT-10}
    27  
    28  __pid_run() {
    29    __pids_var_run $prog $pidfile
    30  }
    31  
    32  __auto_restart_daemon_pid_run() {
    33      unset auto_restart_daemon_pid
    34      if [ ! -f $auto_restart_daemon_pidfile ]; then
    35          return
    36      fi
    37      AUTO_RESTART_DAEMON_PID_NUM=$(cat $auto_restart_daemon_pidfile)
    38      if [[ ! -z "$AUTO_RESTART_DAEMON_PID_NUM" ]]; then
    39          export auto_restart_daemon_pid=$(ps -p $AUTO_RESTART_DAEMON_PID_NUM -o pid=)
    40      fi
    41  }
    42  #------------------------Related Auto restart daemon functions-----------------------------
    43  __auto_restart_daemon() {
    44      local backOffTime=$AUTO_RESTART_INTERVAL
    45      local coeff=2
    46      while :
    47      do
    48          sleep 1
    49          __pid_run
    50          if [ -z "$pid" ]; then
    51              echo "INFO[`date`] node[${PID_NUM}] is down"
    52              if [ -f $pidfile ]; then
    53                  echo "INFO[`date`] remove redundant pid file"
    54                  rm -f ${lockfile} ${pidfile}
    55              fi
    56              echo "INFO[`date`] Sleep for backOffTime.... ${backOffTime} seconds."
    57              sleep $backOffTime
    58              echo -n "INFO[`date`] "
    59              start_node
    60              backOffTime=$(echo $backOffTime $coeff | awk '{printf "%.1f\n",$1*$2}')
    61  
    62              echo "INFO[`date`] backOffTime = ${backOffTime}, Restarted node pid = ${PID_NUM}"
    63              PID_NUM=$(cat $pidfile)
    64              echo ""
    65          fi
    66      done
    67  }
    68  
    69  start_auto_restart_daemon() {
    70      __auto_restart_daemon_pid_run
    71      if [ -z $auto_restart_daemon_pid ]; then
    72          __auto_restart_daemon >> ${LOG_DIR}/restart_daemon.out 2>&1 &
    73          disown
    74          AUTO_RESTART_DAEMON_PID_NUM=$!
    75          AUTO_RESTART_DAEMON_RETVAL=$?
    76  
    77          set +f
    78          if [ $AUTO_RESTART_DAEMON_RETVAL = 0 ]; then
    79              echo $AUTO_RESTART_DAEMON_PID_NUM > ${auto_restart_daemon_pidfile}
    80              echo "Success to start auto restart daemon."
    81          else
    82              echo "Fail to start auto restart daemon."
    83          fi
    84      fi
    85  }
    86  
    87  stop_auto_restart_daemon() {
    88      echo -n "Shutting down auto restart daemon: "
    89      killproc -p ${auto_restart_daemon_pidfile} -d ${STOP_TIMEOUT}
    90      RETVAL=$?
    91      echo
    92      [ $RETVAL = 0 ] && rm -f ${auto_restart_daemon_lockfile} ${auto_restart_daemon_pidfile}
    93  }
    94  
    95  status_auto_restart_daemon() {
    96      __auto_restart_daemon_pid_run
    97      if [ -n "$auto_restart_daemon_pid" ]; then
    98          echo "auto restart daemon is running."
    99      else
   100          echo "auto restart daemon is down."
   101      fi
   102  }
   103  
   104  #------------------------Related to Klaytn node functions-----------------------------
   105  
   106  set -f
   107  OPTIONS=""
   108  
   109  if [[ ! -z $METRICS ]] && [[ $METRICS -eq 1 ]]; then
   110      OPTIONS="$OPTIONS --metrics"
   111  fi
   112  
   113  if [[ ! -z $PROMETHEUS ]] && [[ $PROMETHEUS -eq 1 ]]; then
   114      OPTIONS="$OPTIONS --prometheus"
   115  fi
   116  
   117  if [[ ! -z $DB_NO_PARALLEL_WRITE ]] && [[ $DB_NO_PARALLEL_WRITE -eq 1 ]]; then
   118      OPTIONS="$OPTIONS --db.no-parallel-write"
   119  fi
   120  
   121  if [[ ! -z $MULTICHANNEL ]] && [[ $MULTICHANNEL -eq 1 ]]; then
   122      OPTIONS="$OPTIONS --multichannel"
   123  fi
   124  
   125  if [[ ! -z $RPC_ENABLE ]] && [[ $RPC_ENABLE -eq 1 ]]; then
   126      OPTIONS="$OPTIONS --rpc"
   127      RPC_API=`echo $RPC_API | tr -d "[:space:]"`
   128      if [ ! -z $RPC_API ]; then
   129          OPTIONS="$OPTIONS --rpcapi $RPC_API"
   130      fi
   131      if [ ! -z $RPC_PORT ]; then
   132          OPTIONS="$OPTIONS --rpcport $RPC_PORT"
   133      fi
   134      if [ ! -z $RPC_ADDR ]; then
   135          OPTIONS="$OPTIONS --rpcaddr $RPC_ADDR"
   136      fi
   137      if [ ! -z $RPC_CORSDOMAIN ]; then
   138          OPTIONS="$OPTIONS --rpccorsdomain $RPC_CORSDOMAIN"
   139      fi
   140      if [ ! -z $RPC_VHOSTS ]; then
   141          OPTIONS="$OPTIONS --rpcvhosts $RPC_VHOSTS"
   142      fi
   143  fi
   144  
   145  if [[ ! -z $WS_ENABLE ]] && [[ $WS_ENABLE -eq 1 ]]; then
   146      OPTIONS="$OPTIONS --ws"
   147      WS_API=`echo $WS_API | tr -d "[:space:]"`
   148      if [ ! -z $WS_API ]; then
   149          OPTIONS="$OPTIONS --wsapi $WS_API"
   150      fi
   151      if [ ! -z $WS_PORT ]; then
   152          OPTIONS="$OPTIONS --wsport $WS_PORT"
   153      fi
   154      if [ ! -z $WS_ADDR ]; then
   155          OPTIONS="$OPTIONS --wsaddr $WS_ADDR"
   156      fi
   157      if [ ! -z $WS_ORIGINS ]; then
   158          OPTIONS="$OPTIONS --wsorigins $WS_ORIGINS"
   159      fi
   160  fi
   161  
   162  # Cypress network => NETWORK_ID is null && NETWORK = "cypress"
   163  # Baobab network => NETWORK_ID is null && NETWORK = "baobab"
   164  # Else => private network
   165  if [[ -z $NETWORK_ID ]]; then
   166      if [[ $NETWORK == "baobab" ]]; then
   167          OPTIONS="$OPTIONS --baobab"
   168      elif [[ $NETWORK == "cypress" ]]; then
   169          OPTIONS="$OPTIONS --cypress"
   170      else
   171          echo
   172          echo "[ERROR] network id is not specified and network is not available."
   173          echo "Available network: baobab, cypress"
   174          exit 1
   175      fi
   176  else
   177      OPTIONS="$OPTIONS --networkid $NETWORK_ID"
   178      echo "[INFO] creating a private network: $NETWORK_ID"
   179      if [[ ! -z $NETWORK ]]; then
   180          echo
   181          echo "[WARN] ignoring the specified network: $NETWORK"
   182      fi
   183  fi
   184  
   185  if [ ! -z $DATA_DIR ]; then
   186      OPTIONS="$OPTIONS --datadir $DATA_DIR"
   187  fi
   188  
   189  if [ ! -z $PORT ]; then
   190      OPTIONS="$OPTIONS --port $PORT"
   191  fi
   192  
   193  if [ ! -z $SUBPORT ]; then
   194      OPTIONS="$OPTIONS --subport $SUBPORT"
   195  fi
   196  
   197  if [ ! -z $SERVER_TYPE ]; then
   198      OPTIONS="$OPTIONS --srvtype $SERVER_TYPE"
   199  fi
   200  
   201  if [ ! -z $VERBOSITY ]; then
   202      OPTIONS="$OPTIONS --verbosity $VERBOSITY"
   203  fi
   204  
   205  if [ ! -z $TXPOOL_EXEC_SLOTS_ALL ]; then
   206      OPTIONS="$OPTIONS --txpool.exec-slots.all $TXPOOL_EXEC_SLOTS_ALL"
   207  fi
   208  
   209  if [ ! -z $TXPOOL_NONEXEC_SLOTS_ALL ]; then
   210      OPTIONS="$OPTIONS --txpool.nonexec-slots.all $TXPOOL_NONEXEC_SLOTS_ALL"
   211  fi
   212  
   213  if [ ! -z $TXPOOL_EXEC_SLOTS_ACCOUNT ]; then
   214      OPTIONS="$OPTIONS --txpool.exec-slots.account $TXPOOL_EXEC_SLOTS_ACCOUNT"
   215  fi
   216  
   217  if [ ! -z $TXPOOL_NONEXEC_SLOTS_ACCOUNT ]; then
   218      OPTIONS="$OPTIONS --txpool.nonexec-slots.account $TXPOOL_NONEXEC_SLOTS_ACCOUNT"
   219  fi
   220  
   221  if [ ! -z $TXPOOL_LIFE_TIME ]; then
   222      OPTIONS="$OPTIONS --txpool.lifetime $TXPOOL_LIFE_TIME"
   223  fi
   224  
   225  if [ ! -z $SYNCMODE ]; then
   226      OPTIONS="$OPTIONS --syncmode $SYNCMODE"
   227  fi
   228  
   229  if [ ! -z $MAXCONNECTIONS ]; then
   230      OPTIONS="$OPTIONS --maxconnections $MAXCONNECTIONS"
   231  fi
   232  
   233  if [ ! -z $LDBCACHESIZE ]; then
   234      OPTIONS="$OPTIONS --db.leveldb.cache-size $LDBCACHESIZE"
   235  fi
   236  
   237  if [[ ! -z $NO_DISCOVER ]] && [[ $NO_DISCOVER -eq 1 ]]; then
   238      OPTIONS="$OPTIONS --nodiscover"
   239  fi
   240  
   241  if [[ ! -z $BOOTNODES ]] && [[ $BOOTNODES != "" ]]; then
   242      OPTIONS="$OPTIONS --bootnodes $BOOTNODES"
   243  fi
   244  
   245  if [[ ! -z $ADDITIONAL ]] && [[ $ADDITIONAL != "" ]]; then
   246      OPTIONS="$OPTIONS $ADDITIONAL"
   247  fi
   248  
   249  if [[ ! -z $REWARDBASE ]] && [[ $REWARDBASE != "" ]]; then
   250      OPTIONS="$OPTIONS --rewardbase $REWARDBASE"
   251  fi
   252  
   253  if [[ ! -z $AUTO_RESTART ]] && [[ $AUTO_RESTART -eq 1 ]]; then
   254      OPTIONS="$OPTIONS --autorestart.enable"
   255  fi
   256  
   257  if [[ ! -z $LOG_ROTATE ]] && [[ $LOG_ROTATE -eq 1 ]]; then
   258      OPTIONS="$OPTIONS --log.rotate"
   259      if [[ ! -z $LOG_MAXSIZE ]]; then
   260        OPTIONS="$OPTIONS --log.maxsize $LOG_MAXSIZE"
   261      fi
   262      if [[ ! -z $LOG_MAXBACKUP ]]; then
   263        OPTIONS="$OPTIONS --log.maxbackup $LOG_MAXBACKUP"
   264      fi
   265      if [[ ! -z $LOG_MAXAGE ]]; then
   266        OPTIONS="$OPTIONS --log.maxage $LOG_MAXAGE"
   267      fi
   268      if [[ ! -z $LOG_COMPRESS ]] && [[ $LOG_COMPRESS -eq 1 ]]; then
   269        OPTIONS="$OPTIONS --log.compress"
   270      fi
   271  fi
   272  
   273  BASEDIR="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
   274  CURRENTFILE=`basename "$0"`
   275  OPTIONS="$OPTIONS --autorestart.daemon.path $BASEDIR/$CURRENTFILE"
   276  set +f
   277  
   278  start_node() {
   279      echo -n "Starting $prog: "
   280      if [ ! -d ${LOG_DIR} ]; then
   281              mkdir -p ${LOG_DIR}
   282      fi
   283  
   284      __pids_var_run "$prog" "$pidfile"
   285      [ -n "$pid" ] && echo && return
   286  
   287      set -f
   288      OPTIONS="$OPTIONS --log.file ${LOG_DIR}/kcnd.out"
   289      TERM="dumb" $kcn $OPTIONS >> ${LOG_DIR}/kcnd.out 2>&1 &
   290      RETVAL=$?
   291      PIDNUM=$!
   292      set +f
   293      if [ $RETVAL = 0 ]; then
   294              echo $PIDNUM > ${pidfile}
   295              touch ${lockfile}
   296              success "$prog startup"
   297      else
   298              failure "$prog startup"
   299      fi
   300      echo
   301      return $RETVAL
   302  }
   303  
   304  stop_node() {
   305      echo -n "Shutting down $prog: "
   306      killproc -p ${pidfile} -d ${STOP_TIMEOUT} kcn
   307      RETVAL=$?
   308      echo
   309      [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
   310  }
   311  
   312  #--------------------- Public functions --------------------------
   313  start() {
   314      if [ ! -d $LOG_DIR ]; then
   315          mkdir -p $LOG_DIR
   316      fi
   317  
   318      if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then
   319          start_auto_restart_daemon
   320      else
   321          start_node
   322      fi
   323  }
   324  
   325  stop() {
   326      if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then
   327          stop_auto_restart_daemon
   328      fi
   329      stop_node
   330  }
   331  
   332  case "$1" in
   333      start)
   334          start
   335          ;;
   336      stop)
   337          stop
   338          ;;
   339      status)
   340          status -p ${pidfile} -l ${lockfile} $kcn
   341          if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then
   342              status_auto_restart_daemon
   343          fi
   344          ;;
   345      restart)
   346          stop
   347          start
   348          ;;
   349      *)
   350          echo "Usage: $prog {start|stop|restart|status}"
   351          exit 1
   352          ;;
   353  esac
   354  exit $?