The bitcoind
process exits almost immediately when you run bitcoind-cli stop
so you cannot simply detect the bitcoind
process. Even though you can possibly see bitcoind
with htop
after running bitcoin-cli stop
you cannot see it with ps
because it has exited.
The answer is to find a way to reliably wait for the process cache to finish writing to disk. bitcoind
nicely writes to debug log with the following message as the last thing it sends to disk, Shutdown: done
so it is possible to detect this way.
The following script is available on GitHub on the MIT Licence and is republished here with permission under the additional licences of.
I Hope it is useful for you.
#!/bin/bash
# Exit and restart bitcoind after some time period
# Willtech ©2022
# How often to restart in seconds
timeout=3600
# BAT / CMD goto function
# Source: https://www.codegrepper.com/code-examples/shell/bash+jump+to
function goto
{
label=$1
cmd=$(sed -n "/^:[[:blank:]][[:blank:]]*${label}/{:a;n;p;ba};" $0 |
grep -v ':$')
eval "$cmd"
exit
}
# Just for the heck of it: how to create a variable where to jump to:
start=${1:-"start"}
goto "$start"
: start
#Start bitcoind
bitcoind -daemon
#Run until timout
sleep $timeout
#Stop bitcoind
bitcoin-cli stop
#Tries for ten minutes to see if exit is detected.
until timeout 600s tail -f /media/drive2/.bitcoin/debug.log | grep -m 1 "Shutdown: done"
do
sleep 2
done
#Log again
echo again
#Loop to : start
goto "start"
echo bar
# Ref >>>
## https://askubuntu.com/a/1070296
## https://www.delftstack.com/howto/linux/bash-goto/
Anecdotally, it used to take maybe a month with 1TB USB3 platter disk to do initial sync even with swap as larger than 100GB but now it takes only about three days restarting every one hour until it is up to date. That is in blocks download mode on a blank disk so that data is written near to where it is found.