diff --git a/bin/singlestart.sh b/bin/singlestart.sh index 259463a0ca28d1054696c6a7da19e5f74768592d..a00962e11aeffb5b7eba180ba9924f8afa14369c 100755 --- a/bin/singlestart.sh +++ b/bin/singlestart.sh @@ -9,22 +9,34 @@ bootnodekeyfile=${BFANETWORKDIR}/bootnode/key # Bail out if anything fails. trap "exit 1" ERR -trap "reaper" SIGINT +# Detect children dying +trap "reaper" SIGINT SIGCHLD unset LOGDIR LOGPIPE PIDIDX declare -A PIDIDX function reaper() { - local rc + local wecare=0 for pid in ${!PIDIDX[*]} do kill -0 $pid 2>/dev/null && continue - rc=wait $pid + local rc=0 + wait $pid || rc=$? || true echo "*** ${PIDIDX[$pid]} (pid $pid) has exited with value $rc" unset PIDIDX[$pid] + wecare=1 done + test $wecare = 1 || return # kill all - an extra kill doesn't hurt. I hope. - kill ${!PIDIDX[*]} 2>/dev/null || true + for pid in ${!PIDIDX[*]} + do + # don't kill log.sh, because it may still be logging for us + if [ "${PIDIDX[$pid]}" != "log.sh" ] + then + echo "*** Killing ${PIDIDX[$pid]} (pid $pid)." + kill $pid || true + fi + done max=30 } @@ -73,27 +85,34 @@ LOGPIPE=${LOGDIR}/logpipe mknod ${LOGPIPE} p ${BFAHOME}/bin/log.sh ${BFANODEDIR}/log < ${LOGPIPE} & PIDIDX[$!]="log.sh" +exec > ${LOGPIPE} 2>&1 echo "*** Starting geth." -geth --config ${BFATOML} --gcmode archive --verbosity ${BFAVERBOSITY:-3} --allow-insecure-unlock > ${LOGPIPE} 2>&1 & +geth --config ${BFATOML} --gcmode archive --verbosity ${BFAVERBOSITY:-3} --allow-insecure-unlock & PIDIDX[$!]="geth" # bootnode if [ -r "$bootnodekeyfile" ] then echo "*** Starting bootnode." - bootnode --nodekey $bootnodekeyfile > ${LOGPIPE} 2>&1 & + bootnode --nodekey $bootnodekeyfile & PIDIDX[$!]="bootnode" fi echo "*** Starting monitor.js" -monitor.js > ${LOGPIPE} 2>&1 & +monitor.js & PIDIDX[$!]="monitor.js" max=-1 -while [ -n "${!PIDIDX[*]}" -a $max -ne 0 ] +# Exit if only 1 process remains - we hope it is log.sh, but either way, +# it is time to end. +while [ "${#PIDIDX[*]}" -gt 1 -a $max -ne 0 ] do sleep 1 max=$(( $max - 1 )) done -kill -KILL ${!PIDIDX[*]} 2>/dev/null || true +if [ ${#PIDIDX[*]} -gt 0 ] +then + echo "*** Killing all remaining processes: ${PIDIDX[*]} (${!PIDIDX[*]})." + kill -KILL ${!PIDIDX[*]} 2>/dev/null || true +fi