github.com/klaytn/klaytn@v1.12.1/build/packaging/linux/bin/kbnd (about) 1 #!/usr/bin/env bash 2 3 BIN=$(cd "$(dirname $0)"; pwd) 4 CMD_HOME=$(dirname $BIN) 5 CONF=$CMD_HOME/conf 6 STOP_TIMEOUT=10 # seconds to wait for a clean exit 7 8 source $CONF/kbnd.conf 9 10 pidfile=$CMD_HOME/kbnd.pid 11 auto_restart_daemon_pidfile=$DATA_DIR/restart_daemon_kbnd.pid 12 13 __pid_run() { 14 unset pid 15 if [ ! -f $pidfile ]; then 16 return 17 fi 18 PID_NUM=$(cat $pidfile) 19 if [[ ! -z "$PID_NUM" ]]; then 20 export pid=$(ps -p $PID_NUM -o pid=) 21 fi 22 } 23 24 __auto_restart_daemon_pid_run() { 25 unset auto_restart_daemon_pid 26 if [ ! -f $auto_restart_daemon_pidfile ]; then 27 return 28 fi 29 AUTO_RESTART_DAEMON_PID_NUM=$(cat $auto_restart_daemon_pidfile) 30 if [[ ! -z "$AUTO_RESTART_DAEMON_PID_NUM" ]]; then 31 export auto_restart_daemon_pid=$(ps -p $AUTO_RESTART_DAEMON_PID_NUM -o pid=) 32 fi 33 } 34 35 __kill_timeout() { 36 local PIDNUM=$1 37 kill $PIDNUM 38 for i in `seq 0 100 $((1000 * $STOP_TIMEOUT))`; do 39 if ! kill -0 $PIDNUM 2> /dev/null; then 40 echo "OK" 41 return 42 fi 43 sleep 0.1 44 done 45 kill -9 $PIDNUM && echo "Killed" 46 } 47 48 #------------------------Related Auto restart daemon functions----------------------------- 49 __auto_restart_daemon() { 50 local backOffTime=$AUTO_RESTART_INTERVAL 51 local coeff=2 52 while : 53 do 54 sleep 1 55 __pid_run 56 if [ -z "$pid" ]; then 57 echo "INFO[`date`] node[${PID_NUM}] is down" 58 if [ -f $pidfile ]; then 59 echo "INFO[`date`] remove redundant pid file" 60 rm -f ${pidfile} 61 fi 62 echo "INFO[`date`] Sleep for backOffTime.... ${backOffTime} seconds." 63 sleep $backOffTime 64 echo -n "INFO[`date`] " 65 start_node 66 backOffTime=$(echo $backOffTime $coeff | awk '{printf "%.1f\n",$1*$2}') 67 68 echo "INFO[`date`] backOffTime = ${backOffTime}, Restarted node pid = ${PID_NUM}" 69 PID_NUM=$(cat $pidfile) 70 echo "" 71 fi 72 done 73 } 74 75 start_auto_restart_daemon() { 76 __auto_restart_daemon_pid_run 77 if [ -z $auto_restart_daemon_pid ]; then 78 __auto_restart_daemon >> ${LOG_DIR}/restart_daemon.out 2>&1 & 79 disown 80 AUTO_RESTART_DAEMON_PID_NUM=$! 81 AUTO_RESTART_DAEMON_RETVAL=$? 82 83 set +f 84 if [ $AUTO_RESTART_DAEMON_RETVAL = 0 ]; then 85 echo $AUTO_RESTART_DAEMON_PID_NUM > ${auto_restart_daemon_pidfile} 86 echo "Success to start auto restart daemon." 87 else 88 echo "Fail to start auto restart daemon." 89 fi 90 fi 91 } 92 93 stop_auto_restart_daemon() { 94 __auto_restart_daemon_pid_run 95 [ -z "$auto_restart_daemon_pid" ] && echo "auto restart daemon is not running" && return 96 echo -n "Shutting down auto restart daemon: " 97 __kill_timeout $(cat ${auto_restart_daemon_pidfile}) && rm -f ${auto_restart_daemon_pidfile} 98 } 99 100 status_auto_restart_daemon() { 101 __auto_restart_daemon_pid_run 102 if [ -n "$auto_restart_daemon_pid" ]; then 103 echo "auto restart daemon is running." 104 else 105 echo "auto restart daemon is down." 106 fi 107 } 108 109 #------------------------Related to Klaytn node functions----------------------------- 110 111 __check_option() { 112 if [ ! -d $LOG_DIR ]; then 113 mkdir -p $LOG_DIR 114 fi 115 116 if [ ! -d $DATA_DIR ]; then 117 mkdir -p $DATA_DIR 118 fi 119 120 set -f 121 OPTIONS="--nodekey ${KEY_PATH}" 122 if [[ ! -z $NETWORK_ID ]] && [[ $NETWORK_ID != "" ]]; then 123 OPTIONS="$OPTIONS --networkid $NETWORK_ID" 124 fi 125 126 if [[ ! -z $METRICS ]] && [[ $METRICS -eq 1 ]]; then 127 OPTIONS="$OPTIONS --metrics" 128 fi 129 130 if [[ ! -z $PROMETHEUS ]] && [[ $PROMETHEUS -eq 1 ]]; then 131 OPTIONS="$OPTIONS --prometheus" 132 fi 133 134 if [[ ! -z $AUTHORIZED_NODES ]] && [[ $AUTHORIZED_NODES != "" ]]; then 135 OPTIONS="$OPTIONS --authorized-nodes $AUTHORIZED_NODES" 136 fi 137 138 if [[ ! -z $ADDITIONAL ]] && [[ $ADDITIONAL != "" ]]; then 139 OPTIONS="$OPTIONS $ADDITIONAL" 140 fi 141 142 if [ ! -z $DATA_DIR ]; then 143 OPTIONS="$OPTIONS --datadir $DATA_DIR" 144 fi 145 146 if [[ ! -z $LOG_ROTATE ]] && [[ $LOG_ROTATE -eq 1 ]]; then 147 OPTIONS="$OPTIONS --log.rotate" 148 if [[ ! -z $LOG_MAXSIZE ]]; then 149 OPTIONS="$OPTIONS --log.maxsize $LOG_MAXSIZE" 150 fi 151 if [[ ! -z $LOG_MAXBACKUP ]]; then 152 OPTIONS="$OPTIONS --log.maxbackups $LOG_MAXBACKUP" 153 fi 154 if [[ ! -z $LOG_MAXAGE ]]; then 155 OPTIONS="$OPTIONS --log.maxage $LOG_MAXAGE" 156 fi 157 if [[ ! -z $LOG_COMPRESS ]] && [[ $LOG_COMPRESS -eq 1 ]]; then 158 OPTIONS="$OPTIONS --log.compress" 159 fi 160 fi 161 } 162 163 start_node() { 164 __pid_run 165 [ -n "$pid" ] && echo "kbnd already running...[$pid]" && return 166 167 if [ ! -f $KEY_PATH ]; then 168 echo 169 echo " [ERROR] : KEY_PATH is not defined in $CONF/kbnd.conf" 170 exit 1 171 fi 172 173 echo -n "Starting kbnd: " 174 175 __check_option 176 177 OPTIONS="$OPTIONS --log.file ${LOG_DIR}/kbnd.out" 178 TERM="dumb" $BIN/kbn ${OPTIONS} >> ${LOG_DIR}/kbnd.out 2>&1 & 179 RETVAL=$? 180 PIDNUM=$! 181 set +f 182 if [ $RETVAL = 0 ]; then 183 echo $PIDNUM > ${pidfile} 184 echo "Success to start node." 185 else 186 echo "Fail to start node." 187 fi 188 return $RETVAL 189 } 190 191 stop_node() { 192 __pid_run 193 [ -z "$pid" ] && echo "kbnd is not running" && return 194 echo -n "Shutting down kbnd: " 195 __kill_timeout $(cat ${pidfile}) && rm -f ${pidfile} 196 } 197 198 status_node() { 199 __pid_run 200 if [ -n "$pid" ]; then 201 echo "kbnd is running" 202 else 203 echo "kbnd is down" 204 fi 205 } 206 207 #--------------------- Public functions -------------------------- 208 start() { 209 if [ ! -d $LOG_DIR ]; then 210 mkdir -p $LOG_DIR 211 fi 212 213 if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then 214 start_auto_restart_daemon 215 else 216 start_node 217 fi 218 } 219 220 start_docker() { 221 echo -n "Starting kbnd: " 222 __check_option 223 224 echo "$BIN/kbn $OPTIONS" 225 $BIN/kbn $OPTIONS 226 } 227 228 stop() { 229 if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then 230 stop_auto_restart_daemon 231 fi 232 stop_node 233 } 234 235 236 status() { 237 status_node 238 if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then 239 status_auto_restart_daemon 240 fi 241 } 242 243 restart() { 244 stop 245 sleep 3 246 start 247 } 248 249 case "$1" in 250 start) 251 start 252 ;; 253 start-docker) 254 start_docker 255 ;; 256 stop) 257 stop 258 ;; 259 status) 260 status 261 ;; 262 restart) 263 restart 264 ;; 265 *) 266 echo "Usages: kbnd {start|start-docker|stop|restart|status}" 267 exit 1 268 ;; 269 esac 270 exit 0