Bitcoin Forum

Bitcoin => Bitcoin Technical Support => Topic started by: walletfan on May 04, 2015, 04:41:57 PM



Title: Bitcoin from Shell Script. Loading block index duration
Post by: walletfan on May 04, 2015, 04:41:57 PM
Hey everyone
I start and stop bitcoind inside a shell script. In between I'd like to pass some bitcoin-cli commands. Unfortunatly I have to wait at the beginning of the script for some seconds so that the block index can be loaded. Otherwise the return value for getblockcount (just an example) is error: {"code":-28,"message":"Loading block index..."}
With increasing block number the time to wait increases, too. Hardcoding the wait time with sleep is not an option.
Is there a way to run a command from script if the block index is fully loaded?


Title: Re: Bitcoin from Shell Script. Loading block index duration
Post by: CIYAM on May 04, 2015, 04:46:59 PM
Why don't you just parse the response to check for the error?

(then use your own loop to keep rechecking)


Title: Re: Bitcoin from Shell Script. Loading block index duration
Post by: walletfan on May 04, 2015, 05:26:50 PM
So there is no more elegant way?
Tried this, but until now I don'g get the error message stored in a variable. Should be solvable though.


Title: Re: Bitcoin from Shell Script. Loading block index duration
Post by: CIYAM on May 04, 2015, 05:32:47 PM
So there is no more elegant way?

Not that I am aware of (other than using the "blocknotify" approach).

Tried this, but until now I don'g get the error message stored in a variable. Should be solvable though.

Maybe you just need to learn a bit more about scripting?


Title: Re: Bitcoin from Shell Script. Loading block index duration
Post by: tspacepilot on May 04, 2015, 08:31:20 PM
Code:
reply=`bitcoin-cli mycommand`
while echo $reply | grep -q "Loading block index..."; do
  sleep 10
  reply=`bitcoin-cli mycommand`
done

But you should probably use a json parser and make something cleaner.