In my test environment, which is ssh
,bash
and screen
, I'm launching a monerod
followed by several other programs wallet-rpc/cli
and more, which are all depending on monerod
.
To prevent any confusion, I would like to wait for monerod
to be fully synced, before launching the dependent programs.
Can I RPC the daemon, and get the local blockheight
/ network blockheight
, so I can wait for the sync to complete?
My solution (Updated to wait if daemon is not ready
)
get_info() {
local JSON=$( curl -s -X POST http://127.0.0.1:28081/json_rpc -d '{"jsonrpc":"2.0","id":"0","method":"get_info"}' -H 'Content-Type: application/json' | grep "$1" | grep -o '[0-9]*' )
[ "${JSON}" = "" ] \
|| {
echo ${JSON}
return 0
} \
&& {
return 1
}
}
wait_for_sync() {
local TARGET=0
local HEIGHT=0
local CNT=0
until [[ ${TARGET} -ne 0 ]]
do
((CNT++))
echo -en "${WHITE}Waiting for daemon: ${RED}${CNT}${RESTORE}\r"
sleep 1
TARGET=$(get_info '"target_height"' | grep -o '[0-9]*')
done
echo -ne "\033[2K"
echo -n "${WHITE}Waiting for daemon to sync: ${HEIGHT}/${TARGET}${RESTORE}"
while [ ${HEIGHT} -lt ${TARGET} ]
do
HEIGHT=$(get_info '"height"' | grep -o '[0-9]*')
echo -ne "\r${WHITE}Waiting for daemon to sync: ${HEIGHT}/${TARGET}${RESTORE}"
sleep 1
done
echo " ${GREEN}OK Synced${RESTORE}"
}