user75001: 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.

user75001: 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

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.

75001: <p>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 <code>Error: Cannot obtain a lock on data directory /media/drive2/.bitcoin/. Bitcoin Core is probably already running.</code></p>
<p>How to restart bitcoind automatically using a script without error message and failure to start?</p>
<p>This question is specifically to allow <code>bitcoind</code> to exit cleanly as it can take an inordinate amount of time to exit cleanly even though you cannot see the process with <code>ps -ae | grep bitcoind</code> you can still see it with <code>htop</code> until it finishes committing the process cache to disk and finally &quot;Shutdown: done&quot; is written to disk. If configured as  daemon it is possible to have thousands of failures starting before the process finally exists.</p>
 75001: <p>The <code>bitcoind</code> process exits almost immediately when you run <code>bitcoind-cli stop</code> so you cannot simply detect the <code>bitcoind</code> process. <em>Even though you can possibly see <code>bitcoind</code> with <code>htop</code> after running <code>bitcoin-cli stop</code> you cannot see it with <code>ps</code> because it has exited.</em></p>
<p>The answer is to find a way to reliably wait for the process cache to finish writing to disk. <code>bitcoind</code> nicely writes to debug log with the following message as the last thing it sends to disk, <code>Shutdown: done</code> so it is possible to detect this way.</p>
<p>The following script is available on <a href="https://github.com/Willtech/bitcoin-script/blob/master/runbitcoind.sh" rel="nofollow noreferrer">GitHub</a> on the <a href="https://github.com/Willtech/bitcoin-script/blob/master/LICENSE" rel="nofollow noreferrer">MIT Licence</a> and is republished here with permission under the additional licences of.</p>
<p>I Hope it is useful for you.</p>
<pre><code>#!/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 &quot;/^:[[:blank:]][[:blank:]]*${label}/{:a;n;p;ba};&quot; $0 | 
          grep -v ':$')
    eval &quot;$cmd&quot;
    exit
}
# Just for the heck of it: how to create a variable where to jump to:
start=${1:-&quot;start&quot;}
goto &quot;$start&quot;

: 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 &quot;Shutdown: done&quot;
  do
    sleep 2
  done

#Log again
echo again

#Loop to : start
goto &quot;start&quot;

echo bar
</code></pre>
<p>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.</p>
