1

If, like me you wish to automatically restart bitcoind after some period of time, or if it crashes at least it will restart sometime on it's own you know that it can take a long time on Linux before process cache finishes writing to disk and then you can restart without the dreaded message Error: Cannot obtain a lock on data directory /media/drive2/.bitcoin/. Bitcoin Core is probably already running.

How to restart bitcoind automatically using a script without error message and failure to start?

This question is specifically to allow bitcoind to exit cleanly as it can take an inordinate amount of time to exit cleanly even though you cannot see the process with ps -ae | grep bitcoind you can still see it with htop until it finishes committing the process cache to disk and finally "Shutdown: done" is written to disk. If configured as daemon it is possible to have thousands of failures starting before the process finally exists.

5
  • 1
    Does this answer your question? Ubuntu Linux -- How do I start bitcoind as a service to run automatically? Feb 26, 2022 at 18:15
  • @RedGrittyBrick It would be part answer but it does not safely exit and then restart the process like a process cache flush.
    – Willtech
    Mar 4, 2022 at 12:01
  • Actually I have not checked, I presume that if I configure bitcoind as a service and run systemctl stop bitcoind.service that it returns immediately?
    – Willtech
    Mar 11, 2022 at 15:03
  • The operating system has features designed to do what you want, specifically the respawn or restart`directive is intended to ensure that a service which dies gets restarted ASAP. Where this can be used I think it is often likely to be better than a homebrew solution as probably more time has been spent on the feature by a greater number of people over a much longer period in which bugs and wrinkles have been ironed out. Mar 11, 2022 at 15:08
  • @RedGrittyBrick Oh I agree it is just if systemd understands the difference between exiting clearing application cache and process exited. Let me know if you check I can update my answer.
    – Willtech
    Mar 11, 2022 at 15:30

1 Answer 1

2

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.

1
  • Note you need to set your location of debug.log.
    – Willtech
    Feb 26, 2022 at 3:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.