github.com/klaytn/klaytn@v1.12.1/build/rpm/etc/init.d/kscnd (about) 1 #!/bin/bash 2 # 3 # kscnd Startup script for the kscnd 4 # 5 # chkconfig: - 85 15 6 # description : kscnd is Klaytn consensus node daemon 7 # 8 # processname: kscnd 9 # config: /etc/kscnd/conf/kscnd.conf 10 # pidfile: /var/run/kscnd.pid 11 # 12 13 # Source function library. 14 . /etc/init.d/functions 15 16 if [ -f /etc/kscnd/conf/kscnd.conf ]; then 17 . /etc/kscnd/conf/kscnd.conf 18 fi 19 20 kscn=/usr/bin/kscn 21 prog=kscnd 22 lockfile=${LOCKFILE-/var/lock/subsys/kscnd} 23 pidfile=${PIDFILE-/var/run/kscnd.pid} 24 auto_restart_daemon_pidfile=/var/run/restart_daemon_kscnd.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="--nodiscover" 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 if [ ! -z $NETWORK_ID ]; then 163 OPTIONS="$OPTIONS --networkid $NETWORK_ID" 164 fi 165 166 if [ ! -z $DATA_DIR ]; then 167 OPTIONS="$OPTIONS --datadir $DATA_DIR" 168 fi 169 170 if [ ! -z $PORT ]; then 171 OPTIONS="$OPTIONS --port $PORT" 172 fi 173 174 if [ ! -z $SUBPORT ]; then 175 OPTIONS="$OPTIONS --subport $SUBPORT" 176 fi 177 178 if [ ! -z $SERVER_TYPE ]; then 179 OPTIONS="$OPTIONS --srvtype $SERVER_TYPE" 180 fi 181 182 if [ ! -z $VERBOSITY ]; then 183 OPTIONS="$OPTIONS --verbosity $VERBOSITY" 184 fi 185 186 if [ ! -z $TXPOOL_EXEC_SLOTS_ALL ]; then 187 OPTIONS="$OPTIONS --txpool.exec-slots.all $TXPOOL_EXEC_SLOTS_ALL" 188 fi 189 190 if [ ! -z $TXPOOL_NONEXEC_SLOTS_ALL ]; then 191 OPTIONS="$OPTIONS --txpool.nonexec-slots.all $TXPOOL_NONEXEC_SLOTS_ALL" 192 fi 193 194 if [ ! -z $TXPOOL_EXEC_SLOTS_ACCOUNT ]; then 195 OPTIONS="$OPTIONS --txpool.exec-slots.account $TXPOOL_EXEC_SLOTS_ACCOUNT" 196 fi 197 198 if [ ! -z $TXPOOL_NONEXEC_SLOTS_ACCOUNT ]; then 199 OPTIONS="$OPTIONS --txpool.nonexec-slots.account $TXPOOL_NONEXEC_SLOTS_ACCOUNT" 200 fi 201 202 if [ ! -z $TXPOOL_LIFE_TIME ]; then 203 OPTIONS="$OPTIONS --txpool.lifetime $TXPOOL_LIFE_TIME" 204 fi 205 206 if [ ! -z $SYNCMODE ]; then 207 OPTIONS="$OPTIONS --syncmode $SYNCMODE" 208 fi 209 210 if [ ! -z $MAXCONNECTIONS ]; then 211 OPTIONS="$OPTIONS --maxconnections $MAXCONNECTIONS" 212 fi 213 214 if [ ! -z $LDBCACHESIZE ]; then 215 OPTIONS="$OPTIONS --db.leveldb.cache-size $LDBCACHESIZE" 216 fi 217 218 if [[ ! -z $SCSIGNER ]] && [[ $SCSIGNER != "" ]]; then 219 OPTIONS="$OPTIONS --scsigner $SCSIGNER --unlock $SCSIGNER" 220 fi 221 222 if [[ ! -z $SCSIGNER_PASSWD_FILE ]] && [[ $SCSIGNER_PASSWD_FILE != "" ]]; then 223 OPTIONS="$OPTIONS --password $SCSIGNER_PASSWD_FILE" 224 fi 225 226 if [[ ! -z $SC_MAIN_BRIDGE ]] && [[ $SC_MAIN_BRIDGE -eq 1 ]]; then 227 OPTIONS="$OPTIONS --mainbridge --mainbridgeport $SC_MAIN_BRIDGE_PORT" 228 if [[ ! -z $SC_MAIN_BRIDGE_INDEXING ]] && [[ $SC_MAIN_BRIDGE_INDEXING -eq 1 ]]; then 229 OPTIONS="$OPTIONS --childchainindexing" 230 fi 231 fi 232 233 if [[ ! -z $SC_SUB_BRIDGE ]] && [[ $SC_SUB_BRIDGE -eq 1 ]]; then 234 OPTIONS="$OPTIONS --subbridge --subbridgeport $SC_SUB_BRIDGE_PORT --chaintxperiod $SC_ANCHORING_PERIOD --chaintxlimit $SC_TX_LIMIT " 235 OPTIONS="$OPTIONS --parentchainid $SC_PARENT_CHAIN_ID" 236 if [[ ! -z $SC_ANCHORING ]] && [[ $SC_ANCHORING -eq 1 ]]; then 237 OPTIONS="$OPTIONS --anchoring" 238 fi 239 240 if [[ ! -z $SC_KAS_ANCHOR ]] && [[ $SC_KAS_ANCHOR -eq 1 ]]; then 241 OPTIONS="$OPTIONS --kas.sc.anchor" 242 243 if [[ ! -z $SC_KAS_ANCHOR_PERIOD ]]; then 244 OPTIONS="$OPTIONS --kas.sc.anchor.period $SC_KAS_ANCHOR_PERIOD" 245 fi 246 247 if [[ -z $SC_KAS_ANCHOR_URL ]]; then 248 echo 249 echo "[ERROR] kas.sc.anchor.url should be specified for KAS Anchor." 250 exit 1 251 fi 252 OPTIONS="$OPTIONS --kas.sc.anchor.url $SC_KAS_ANCHOR_URL" 253 254 if [[ -z $SC_KAS_ANCHOR_OPERATOR ]]; then 255 echo 256 echo "[ERROR] kas.sc.anchor.operator should be specified for KAS Anchor." 257 exit 1 258 fi 259 OPTIONS="$OPTIONS --kas.sc.anchor.operator $SC_KAS_ANCHOR_OPERATOR" 260 261 if [[ -z $SC_KAS_ANCHOR_ACCESS_KEY ]]; then 262 echo 263 echo "[ERROR] kas.accesskey should be specified for KAS Anchor." 264 exit 1 265 fi 266 OPTIONS="$OPTIONS --kas.accesskey $SC_KAS_ANCHOR_ACCESS_KEY" 267 268 if [[ -z $SC_KAS_ANCHOR_SECRET_KEY ]]; then 269 echo 270 echo "[ERROR] kas.secretkey should be specified for KAS Anchor." 271 exit 1 272 fi 273 OPTIONS="$OPTIONS --kas.secretkey $SC_KAS_ANCHOR_SECRET_KEY" 274 275 if [[ -z $SC_KAS_ANCHOR_X_CHAIN_ID ]]; then 276 echo 277 echo "[ERROR] kas.x-chain-id should be specified for KAS Anchor." 278 exit 1 279 fi 280 OPTIONS="$OPTIONS --kas.x-chain-id $SC_KAS_ANCHOR_X_CHAIN_ID" 281 282 if [ ! -z $SC_KAS_ANCHOR_REQUEST_TIMEOUT ]; then 283 OPTIONS="$OPTIONS --kas.sc.anchor.request.timeout $SC_KAS_ANCHOR_REQUEST_TIMEOUT" 284 fi 285 fi 286 fi 287 288 if [ ! -z $SC_PARENT_OPERATOR_GASLIMIT ]; then 289 OPTIONS="$OPTIONS --sc.parentoperator.gaslimit $SC_PARENT_OPERATOR_GASLIMIT" 290 fi 291 292 if [ ! -z $SC_CHILD_OPERATOR_GASLIMIT ]; then 293 OPTIONS="$OPTIONS --sc.childoperator.gaslimit $SC_CHILD_OPERATOR_GASLIMIT" 294 fi 295 296 if [[ (! -z $VTRECOVERY && $VTRECOVERY -eq 1 ) || (! -z $SC_VTRECOVERY && $SC_VTRECOVERY -eq 1) ]]; then 297 OPTIONS="$OPTIONS --vtrecovery" 298 if [[ ! -z $SC_VTRECOVERY_INTERVAL ]]; then 299 OPTIONS="$OPTIONS --vtrecoveryinterval $SC_VTRECOVERY_INTERVAL" 300 fi 301 fi 302 303 if [[ ! -z $ADDITIONAL ]] && [[ $ADDITIONAL != "" ]]; then 304 OPTIONS="$OPTIONS $ADDITIONAL" 305 fi 306 307 if [[ ! -z $AUTO_RESTART ]] && [[ $AUTO_RESTART -eq 1 ]]; then 308 OPTIONS="$OPTIONS --autorestart.enable" 309 fi 310 311 if [[ ! -z $LOG_ROTATE ]] && [[ $LOG_ROTATE -eq 1 ]]; then 312 OPTIONS="$OPTIONS --log.rotate" 313 if [[ ! -z $LOG_MAXSIZE ]]; then 314 OPTIONS="$OPTIONS --log.maxsize $LOG_MAXSIZE" 315 fi 316 if [[ ! -z $LOG_MAXBACKUP ]]; then 317 OPTIONS="$OPTIONS --log.maxbackup $LOG_MAXBACKUP" 318 fi 319 if [[ ! -z $LOG_MAXAGE ]]; then 320 OPTIONS="$OPTIONS --log.maxage $LOG_MAXAGE" 321 fi 322 if [[ ! -z $LOG_COMPRESS ]] && [[ $LOG_COMPRESS -eq 1 ]]; then 323 OPTIONS="$OPTIONS --log.compress" 324 fi 325 fi 326 327 BASEDIR="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" 328 CURRENTFILE=`basename "$0"` 329 OPTIONS="$OPTIONS --autorestart.daemon.path $BASEDIR/$CURRENTFILE" 330 set +f 331 332 start_node() { 333 echo -n "Starting $prog: " 334 if [ ! -d ${LOG_DIR} ]; then 335 mkdir -p ${LOG_DIR} 336 fi 337 338 __pids_var_run "$prog" "$pidfile" 339 [ -n "$pid" ] && echo && return 340 341 set -f 342 OPTIONS="$OPTIONS --log.file ${LOG_DIR}/kscnd.out" 343 TERM="dumb" $kscn $OPTIONS >> ${LOG_DIR}/kscnd.out 2>&1 & 344 RETVAL=$? 345 PIDNUM=$! 346 set +f 347 if [ $RETVAL = 0 ]; then 348 echo $PIDNUM > ${pidfile} 349 touch ${lockfile} 350 success "$prog startup" 351 else 352 failure "$prog startup" 353 fi 354 echo 355 return $RETVAL 356 } 357 358 stop_node() { 359 echo -n "Shutting down $prog: " 360 killproc -p ${pidfile} -d ${STOP_TIMEOUT} kscn 361 RETVAL=$? 362 echo 363 [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile} 364 } 365 366 #--------------------- Public functions -------------------------- 367 start() { 368 if [ ! -d $LOG_DIR ]; then 369 mkdir -p $LOG_DIR 370 fi 371 372 if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then 373 start_auto_restart_daemon 374 else 375 start_node 376 fi 377 } 378 379 stop() { 380 if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then 381 stop_auto_restart_daemon 382 fi 383 stop_node 384 } 385 386 case "$1" in 387 start) 388 start 389 ;; 390 stop) 391 stop 392 ;; 393 status) 394 status -p ${pidfile} -l ${lockfile} $kscn 395 if [ ! -z $AUTO_RESTART_NODE ] && [[ $AUTO_RESTART_NODE -eq 1 ]]; then 396 status_auto_restart_daemon 397 fi 398 ;; 399 restart) 400 stop 401 start 402 ;; 403 *) 404 echo "Usage: $prog {start|stop|restart|status}" 405 exit 1 406 ;; 407 esac 408 exit $?