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

     1  #!/bin/bash
     2  #
     3  # kend         Startup script for the ken
     4  #
     5  # chkconfig: - 85 15
     6  # description : kend is the Klaytn endpoint node daemon
     7  #
     8  # processname: kend
     9  # config:  /etc/kend/conf/kend.conf
    10  # pidfile: /var/run/kend.pid
    11  #
    12  
    13  # Source function library.
    14  . /etc/init.d/functions
    15  
    16  if [ -f /etc/kend/conf/kend.conf ]; then
    17          . /etc/kend/conf/kend.conf
    18  fi
    19  
    20  ken=/usr/bin/ken
    21  prog=kend
    22  lockfile=${LOCKFILE-/var/lock/subsys/kend}
    23  pidfile=${PIDFILE-/var/run/kend.pid}
    24  auto_restart_daemon_pidfile=/var/run/restart_daemon_kend.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      if [ ! -z $RPC_CONCURRENCYLIMIT ]; then
   144          OPTIONS="$OPTIONS --rpc.concurrencylimit $RPC_CONCURRENCYLIMIT"
   145      fi
   146      if [ ! -z $RPC_READ_TIMEOUT ]; then
   147          OPTIONS="$OPTIONS --rpcreadtimeout $RPC_READ_TIMEOUT"
   148      fi
   149      if [ ! -z $RPC_WRITE_TIMEOUT ]; then
   150          OPTIONS="$OPTIONS --rpcwritetimeout $RPC_WRITE_TIMEOUT"
   151      fi
   152      if [ ! -z $RPC_IDLE_TIMEOUT ]; then
   153          OPTIONS="$OPTIONS --rpcidletimeout $RPC_IDLE_TIMEOUT"
   154      fi
   155      if [ ! -z $RPC_EXECUTION_TIMEOUT ]; then
   156          OPTIONS="$OPTIONS --rpcexecutiontimeout $RPC_EXECUTION_TIMEOUT"
   157      fi
   158  fi
   159  
   160  if [[ ! -z $WS_ENABLE ]] && [[ $WS_ENABLE -eq 1 ]]; then
   161      OPTIONS="$OPTIONS --ws"
   162      WS_API=`echo $WS_API | tr -d "[:space:]"`
   163      if [ ! -z $WS_API ]; then
   164          OPTIONS="$OPTIONS --wsapi $WS_API"
   165      fi
   166      if [ ! -z $WS_PORT ]; then
   167          OPTIONS="$OPTIONS --wsport $WS_PORT"
   168      fi
   169      if [ ! -z $WS_ADDR ]; then
   170          OPTIONS="$OPTIONS --wsaddr $WS_ADDR"
   171      fi
   172      if [ ! -z $WS_ORIGINS ]; then
   173          OPTIONS="$OPTIONS --wsorigins $WS_ORIGINS"
   174      fi
   175  fi
   176  
   177  # Cypress network => NETWORK_ID is null && NETWORK = "cypress"
   178  # Baobab network => NETWORK_ID is null && NETWORK = "baobab"
   179  # Else => private network
   180  if [[ -z $NETWORK_ID ]]; then
   181      if [[ $NETWORK == "baobab" ]]; then
   182          OPTIONS="$OPTIONS --baobab"
   183      elif [[ $NETWORK == "cypress" ]]; then
   184          OPTIONS="$OPTIONS --cypress"
   185      else
   186          echo
   187          echo "[ERROR] network id is not specified and network is not available."
   188          echo "Available network: baobab, cypress"
   189          exit 1
   190      fi
   191  else
   192      OPTIONS="$OPTIONS --networkid $NETWORK_ID"
   193      echo "[INFO] creating a private network: $NETWORK_ID"
   194      if [[ ! -z $NETWORK ]]; then
   195          echo
   196          echo "[WARN] ignoring the specified network: $NETWORK"
   197      fi
   198  fi
   199  
   200  if [ ! -z $DATA_DIR ]; then
   201      OPTIONS="$OPTIONS --datadir $DATA_DIR"
   202  fi
   203  
   204  if [ ! -z $PORT ]; then
   205      OPTIONS="$OPTIONS --port $PORT"
   206  fi
   207  
   208  if [ ! -z $SUBPORT ]; then
   209      OPTIONS="$OPTIONS --subport $SUBPORT"
   210  fi
   211  
   212  if [ ! -z $SERVER_TYPE ]; then
   213      OPTIONS="$OPTIONS --srvtype $SERVER_TYPE"
   214  fi
   215  
   216  if [ ! -z $VERBOSITY ]; then
   217      OPTIONS="$OPTIONS --verbosity $VERBOSITY"
   218  fi
   219  
   220  if [ ! -z $TXPOOL_EXEC_SLOTS_ALL ]; then
   221      OPTIONS="$OPTIONS --txpool.exec-slots.all $TXPOOL_EXEC_SLOTS_ALL"
   222  fi
   223  
   224  if [ ! -z $TXPOOL_NONEXEC_SLOTS_ALL ]; then
   225      OPTIONS="$OPTIONS --txpool.nonexec-slots.all $TXPOOL_NONEXEC_SLOTS_ALL"
   226  fi
   227  
   228  if [ ! -z $TXPOOL_EXEC_SLOTS_ACCOUNT ]; then
   229      OPTIONS="$OPTIONS --txpool.exec-slots.account $TXPOOL_EXEC_SLOTS_ACCOUNT"
   230  fi
   231  
   232  if [ ! -z $TXPOOL_NONEXEC_SLOTS_ACCOUNT ]; then
   233      OPTIONS="$OPTIONS --txpool.nonexec-slots.account $TXPOOL_NONEXEC_SLOTS_ACCOUNT"
   234  fi
   235  
   236  if [ ! -z $TXPOOL_LIFE_TIME ]; then
   237      OPTIONS="$OPTIONS --txpool.lifetime $TXPOOL_LIFE_TIME"
   238  fi
   239  
   240  if [ ! -z $SYNCMODE ]; then
   241      OPTIONS="$OPTIONS --syncmode $SYNCMODE"
   242  fi
   243  
   244  if [ ! -z $MAXCONNECTIONS ]; then
   245      OPTIONS="$OPTIONS --maxconnections $MAXCONNECTIONS"
   246  fi
   247  
   248  if [ ! -z $LDBCACHESIZE ]; then
   249      OPTIONS="$OPTIONS --db.leveldb.cache-size $LDBCACHESIZE"
   250  fi
   251  
   252  if [[ ! -z $SC_MAIN_BRIDGE ]] && [[ $SC_MAIN_BRIDGE -eq 1 ]]; then
   253      OPTIONS="$OPTIONS --mainbridge --mainbridgeport $SC_MAIN_BRIDGE_PORT"
   254      if [[ ! -z $SC_MAIN_BRIDGE_INDEXING ]] && [[ $SC_MAIN_BRIDGE_INDEXING -eq 1 ]]; then
   255          OPTIONS="$OPTIONS --childchainindexing"
   256      fi
   257  fi
   258  
   259  if [[ ! -z $NO_DISCOVER ]] && [[ $NO_DISCOVER -eq 1 ]]; then
   260      OPTIONS="$OPTIONS --nodiscover"
   261  fi
   262  
   263  if [[ ! -z $BOOTNODES ]] && [[ $BOOTNODES != "" ]]; then
   264      OPTIONS="$OPTIONS --bootnodes $BOOTNODES"
   265  fi
   266  
   267  if [[ ! -z $ADDITIONAL ]] && [[ $ADDITIONAL != "" ]]; then
   268      OPTIONS="$OPTIONS $ADDITIONAL"
   269  fi
   270  
   271  if [[ ! -z $AUTO_RESTART ]] && [[ $AUTO_RESTART -eq 1 ]]; then
   272      OPTIONS="$OPTIONS --autorestart.enable"
   273  fi
   274  
   275  if [[ ! -z $LOG_ROTATE ]] && [[ $LOG_ROTATE -eq 1 ]]; then
   276      OPTIONS="$OPTIONS --log.rotate"
   277      if [[ ! -z $LOG_MAXSIZE ]]; then
   278        OPTIONS="$OPTIONS --log.maxsize $LOG_MAXSIZE"
   279      fi
   280      if [[ ! -z $LOG_MAXBACKUP ]]; then
   281        OPTIONS="$OPTIONS --log.maxbackup $LOG_MAXBACKUP"
   282      fi
   283      if [[ ! -z $LOG_MAXAGE ]]; then
   284        OPTIONS="$OPTIONS --log.maxage $LOG_MAXAGE"
   285      fi
   286      if [[ ! -z $LOG_COMPRESS ]] && [[ $LOG_COMPRESS -eq 1 ]]; then
   287        OPTIONS="$OPTIONS --log.compress"
   288      fi
   289  fi
   290  
   291  BASEDIR="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
   292  CURRENTFILE=`basename "$0"`
   293  OPTIONS="$OPTIONS --autorestart.daemon.path $BASEDIR/$CURRENTFILE"
   294  set +f
   295  
   296  start_node() {
   297      echo -n "Starting $prog: "
   298      if [ ! -d ${LOG_DIR} ]; then
   299              mkdir -p ${LOG_DIR}
   300      fi
   301  
   302      __pids_var_run "$prog" "$pidfile"
   303      [ -n "$pid" ] && echo && return
   304  
   305      set -f
   306      OPTIONS="$OPTIONS --log.file ${LOG_DIR}/kend.out"
   307      TERM="dumb" $ken $OPTIONS >> ${LOG_DIR}/kend.out 2>&1 &
   308      RETVAL=$?
   309      PIDNUM=$!
   310      set +f
   311      if [ $RETVAL = 0 ]; then
   312              echo $PIDNUM > ${pidfile}
   313              touch ${lockfile}
   314              success "$prog startup"
   315      else
   316              failure "$prog startup"
   317      fi
   318      echo
   319      return $RETVAL
   320  }
   321  
   322  stop_node() {
   323      echo -n "Shutting down $prog: "
   324      killproc -p ${pidfile} -d ${STOP_TIMEOUT} $ken
   325      RETVAL=$?
   326      echo
   327      [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
   328  }
   329  
   330  #--------------------- Public functions --------------------------
   331  start() {
   332      if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then
   333          start_auto_restart_daemon
   334      else
   335          start_node
   336      fi
   337  }
   338  
   339  stop() {
   340      if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then
   341          stop_auto_restart_daemon
   342      fi
   343      stop_node
   344  }
   345  
   346  case "$1" in
   347      start)
   348          start
   349          ;;
   350      stop)
   351          stop
   352          ;;
   353      status)
   354          status -p ${pidfile} -l ${lockfile} $ken
   355          if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then
   356            status_auto_restart_daemon
   357          fi
   358          ;;
   359      restart)
   360          stop
   361          start
   362          ;;
   363      *)
   364          echo "Usage: $prog {start|stop|restart|status}"
   365          exit 1
   366          ;;
   367  esac
   368  exit $?