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.
bitcoindas a service and runsystemctl stop bitcoind.servicethat it returns immediately? – Willtech Mar 11 '22 at 15:03respawnorrestart`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. – RedGrittyBrick Mar 11 '22 at 15:08systemdunderstands the difference between exiting clearing application cache and process exited. Let me know if you check I can update my answer. – Willtech Mar 11 '22 at 15:30