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