github.com/klaytn/klaytn@v1.12.1/build/rpm/etc/init.d/kbnd (about) 1 #!/bin/bash 2 # 3 # kbnd Startup script for the kbn 4 # 5 # chkconfig: - 85 15 6 # description : kbnd is Klaytn boot node daemon 7 # 8 # processname: kbnd 9 # config: /etc/kbnd/conf/kbnd.conf 10 # pidfile: /var/run/kbnd.pid 11 # 12 13 # Source function library. 14 . /etc/init.d/functions 15 16 if [ -f /etc/kbnd/conf/kbnd.conf ]; then 17 . /etc/kbnd/conf/kbnd.conf 18 fi 19 20 kbn=/usr/bin/kbn 21 prog=kbnd 22 lockfile=${LOCKFILE-/var/lock/subsys/kbnd} 23 pidfile=${PIDFILE-/var/run/kbnd.pid} 24 auto_restart_daemon_pidfile=/var/run/restart_daemon_kbnd.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 start_node() { 107 echo -n "Starting $prog: " 108 if [ ! -d ${LOG_DIR} ]; then 109 mkdir -p ${LOG_DIR} 110 fi 111 112 if [ ! -d $DATA_DIR ]; then 113 mkdir -p $DATA_DIR 114 fi 115 116 __pids_var_run "$prog" "$pidfile" 117 [ -n "$pid" ] && echo && return 118 119 if [ ! -f $KEY_PATH ]; then 120 echo 121 echo " [ERROR] : KEY_PATH is not defined in /etc/kbnd/conf/kbnd.conf" 122 exit 1 123 fi 124 125 set -f 126 OPTIONS="--nodekey ${KEY_PATH}" 127 if [[ ! -z $NETWORK_ID ]] && [[ $NETWORK_ID != "" ]]; then 128 OPTIONS="$OPTIONS --networkid $NETWORK_ID" 129 fi 130 131 if [[ ! -z $METRICS ]] && [[ $METRICS -eq 1 ]]; then 132 OPTIONS="$OPTIONS --metrics" 133 fi 134 135 if [[ ! -z $PROMETHEUS ]] && [[ $PROMETHEUS -eq 1 ]]; then 136 OPTIONS="$OPTIONS --prometheus" 137 fi 138 139 if [ ! -z $DATA_DIR ]; then 140 OPTIONS="$OPTIONS --datadir $DATA_DIR" 141 fi 142 143 if [[ ! -z $AUTHORIZED_NODES ]] && [[ $AUTHORIZED_NODES != "" ]]; then 144 OPTIONS="$OPTIONS --authorized-nodes $AUTHORIZED_NODES" 145 fi 146 147 if [[ ! -z $ADDITIONAL ]] && [[ $ADDITIONAL != "" ]]; then 148 OPTIONS="$OPTIONS $ADDITIONAL" 149 fi 150 151 if [[ ! -z $LOG_ROTATE ]] && [[ $LOG_ROTATE -eq 1 ]]; then 152 OPTIONS="$OPTIONS --log.rotate" 153 if [[ ! -z $LOG_MAXSIZE ]]; then 154 OPTIONS="$OPTIONS --log.maxsize $LOG_MAXSIZE" 155 fi 156 if [[ ! -z $LOG_MAXBACKUP ]]; then 157 OPTIONS="$OPTIONS --log.maxbackup $LOG_MAXBACKUP" 158 fi 159 if [[ ! -z $LOG_MAXAGE ]]; then 160 OPTIONS="$OPTIONS --log.maxage $LOG_MAXAGE" 161 fi 162 if [[ ! -z $LOG_COMPRESS ]] && [[ $LOG_COMPRESS -eq 1 ]]; then 163 OPTIONS="$OPTIONS --log.compress" 164 fi 165 fi 166 167 OPTIONS="$OPTIONS --log.file ${LOG_DIR}/kbnd.out" 168 TERM="dumb" $kbn ${OPTIONS} >> ${LOG_DIR}/kbnd.out 2>&1 & 169 RETVAL=$? 170 PIDNUM=$! 171 set +f 172 if [ $RETVAL = 0 ]; then 173 echo $PIDNUM > ${pidfile} 174 touch ${lockfile} 175 success "$prog startup" 176 else 177 failure "$prog startup" 178 fi 179 echo 180 return $RETVAL 181 } 182 183 stop_node() { 184 echo -n "Shutting down $prog: " 185 killproc -p ${pidfile} -d ${STOP_TIMEOUT} kbn 186 RETVAL=$? 187 echo 188 [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile} 189 } 190 191 #--------------------- Public functions -------------------------- 192 start() { 193 if [ ! -d $LOG_DIR ]; then 194 mkdir -p $LOG_DIR 195 fi 196 197 if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then 198 start_auto_restart_daemon 199 else 200 start_node 201 fi 202 } 203 204 stop() { 205 if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then 206 stop_auto_restart_daemon 207 fi 208 stop_node 209 } 210 211 case "$1" in 212 start) 213 start 214 ;; 215 stop) 216 stop 217 ;; 218 status) 219 status -p ${pidfile} -l ${lockfile} $kbn 220 if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then 221 status_auto_restart_daemon 222 fi 223 ;; 224 restart) 225 stop 226 sleep 3 227 start 228 ;; 229 *) 230 echo "Usage: $prog {start|stop|restart|status}" 231 exit 1 232 ;; 233 esac 234 exit $?