So i changed the old "$DAEMON stop" method in do_stop().
DATA_DIR=/home/bitcoin/.bitcoind
BITCOINCLI=/home/bitcoin/.bitcoind/bin/bitcoin-cli
stop_daemon_by_cli () {
#[ ! -e "$PIDFILE" ] && echo "Bitcoind not running!" && return 0 #Optional (see below)
$BITCOINCLI -datadir=$DATA_DIR -rpcwait stop
return 0
}
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
stop_daemon_by_cli
start-stop-daemon --stop --quiet --chuid $CHUID --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
RETVAL="$?"
[ "$RETVAL" = 2 ] && return 2
# Wait for children to finish too if this is a daemon that forks
# and if the daemon is only ever run from this initscript.
# If the above conditions are not satisfied then add some other code
# that waits for the process to drop all resources that could be
# needed by services started subsequently. A last resort is to
# sleep for some time.
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
[ "$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f $PIDFILE
return "$RETVAL"
}
Normally you do not need to set -datadir. This is only about my mistake to use .bincoind folder instead of .bitcoin.
The check if pidfile exists in stop_daemon_by_cli() don't work when PIDFILE=/var/run/$NAME. I don't know why the start-stop-daemon create it in datadir and not in /var/run. I had to change the PIDFILE= to work.
Also added -rpcwait because it drops an error if the shutdown is to early after starting bitcoind (e.g. sending stop before rpc listener is up...).
If you have problems using bitcoind over tor when bitcoind ist started before tor, add insserv for tor and use:
# Required-Start: $remote_fs $tor
in your init script.
Debian implement systemd in the next version (v8). Up from then, this way here would be deprecated.
I hope this could help somebody.