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