Author

Topic: Ubuntu/Debian startup script (Read 27543 times)

newbie
Activity: 1
Merit: 0
March 20, 2015, 06:04:30 PM
#16
I build v0.10 today and got this error:

Quote
Error: There is no RPC client functionality in bitcoind anymore. Use the bitcoin-cli utility instead.

So i changed the old "$DAEMON stop" method in do_stop().


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


member
Activity: 66
Merit: 10
February 24, 2015, 09:54:02 PM
#15
Thanks for this, I had this idea today and someone already beat me to it.

I did create an issue, see https://github.com/bitcoin/bitcoin/issues/4124


Great, thanks for sharing this!
vip
Activity: 1358
Merit: 1000
AKA: gigavps
February 20, 2015, 06:34:17 PM
#14
In case anyone is looking for a more robust init file, it is now included in the source for bitcoin.

https://raw.githubusercontent.com/bitcoin/bitcoin/master/contrib/init/bitcoind.conf
hero member
Activity: 812
Merit: 1022
No Maps for These Territories
May 04, 2014, 06:11:28 AM
#13
Thanks for this, I had this idea today and someone already beat me to it.

I did create an issue, see https://github.com/bitcoin/bitcoin/issues/4124
full member
Activity: 128
Merit: 100
I'm doin' fine on cloud 9
February 07, 2013, 11:44:02 AM
#12

Thanks for that, Giga!

-p


FYI in case anyone is trying to get phungus' upstart script working on Ubuntu 12.04. The runlevel config needed to change a bit. See below.

Code:
# bitcoind upstart script for Ubuntu

description "Bitcoin daemon"

start on runlevel [2345]
stop on runlevel [!2345]

respawn
exec su -c "/home/bitcoinuser/bin/bitcoind" - bitcoinuser
vip
Activity: 1358
Merit: 1000
AKA: gigavps
February 06, 2013, 07:45:03 AM
#11
FYI in case anyone is trying to get phungus' upstart script working on Ubuntu 12.04. The runlevel config needed to change a bit. See below.

Code:
# bitcoind upstart script for Ubuntu

description "Bitcoin daemon"

start on runlevel [2345]
stop on runlevel [!2345]

respawn
exec su -c "/home/bitcoinuser/bin/bitcoind" - bitcoinuser
full member
Activity: 128
Merit: 100
I'm doin' fine on cloud 9
July 10, 2011, 07:20:44 PM
#10

Well, I didn't want to go through the trouble of setting up a headless bitcoind the old Debian way. Ubuntu uses upstart now and the scripts are a whole lot easier to write. Here is a quick way to get the current incarnations of bitcoind to behave easily, without passing command line arguments (makes troubleshooting with bitcoind from the command line easy if you want to give the user a shell). I'm including all the steps I use just to help others out. I just dump the tar contents of the latest bitcoin archive into the home directory and then lock down the permissions. In this way, the binaries are out of the way and owned by a system account with no password.

Note: this would be for a new installation. Skip the useradd, chmods, and config files and head straight for the bitcoind.conf upstart script below and modify if you like the way you setup the binaries already (/usr/local/bin, /opt, etc).


## Assumes no bitcoin user, bitcoin tarfile needs to be downloaded, and you're logged in as a sudo-capable user.


sudo useradd -r -m -s /bin/sh bitcoinuser
sudo su -c /bin/bash - bitcoinuser

## Replace all occurrences of 0.3.24 with current bitcoin version

curl -L -O http://sourceforge.net/projects/bitcoin/files/Bitcoin/bitcoin-0.3.24/bitcoin-0.3.24-linux.tar.gz/download
tar zxf bitcoin-0.3.24-linux.tar.gz

mkdir ~/bin
ln -s -t ~/bin ~/bitcoin-0.3.24/bin/32/bitcoin  ## <- Change 32 to 64 if you're on 64bit Linux
ln -s -t ~/bin ~/bitcoin-0.3.24/bin/32/bitcoind  ## <- Change 32 to 64 if you're on 64bit Linux
chmod -R 700 ~/bitcoin-0.3.24

mkdir ~/.bitcoin

cat <# Simple Bitcoin configuration
#server=0 if you use the GUI
server=1
rpcuser=yourrpcuser
rpcpassword=SomethingReallyLongLikeThisfe2yft2Ojy4PYwt0bLIknc29jUz1IVgzMYYtLLH27oXQv
rpctimeout=30
# Change if you need more access
rpcallowip=127.0.0.1
rpcport=8332
gen=0

#Uncomment for TOR proxy
#proxy=127.0.0.1:9050
EOF


chmod 700 ~
chmod 700 ~/.bitcoin
chmod 400 ~bitcoinuser/.bitcoin/bitcoin.conf

chown -R bitcoinuser.bitcoinuser ~

exit


# Write out system upstart script

cat <# bitcoind upstart script for Ubuntu

description "Bitcoin daemon"

start on runlevel 2
start on runlevel 3
start on runlevel 4
start on runlevel 5

stop on runlevel 0
stop on runlevel 1
stop on runlevel 6

respawn
exec su -c "/home/bitcoinuser/bin/bitcoind" - bitcoinuser

EOF



# Now start bitcoind

sudo start bitcoind


# All done!

member
Activity: 70
Merit: 10
July 08, 2011, 12:30:02 AM
#9
Why does it ask for the bitcoin.conf file in "/.bitcoin/bitcoin.conf" when the file is clearly in "~/.bitcoin/bitcoin.conf"?
newbie
Activity: 20
Merit: 0
June 22, 2011, 10:24:38 AM
#8
Stopping bitcoind is done by sending a signal to the daemon. I don't know how good bitcoind tries to close its databases, but I've had once a database corruption when killing the daemon. On the bitcoin chat people advised me to to send the rpc stop command to stop it (and call start-stop-daemon later again to clean it up), checking the debug log can give some information if it is closing right (flushing the database files). Best would be that bitcoind closes itself properly after receiving a kill signal.

How can I implement this to the skript?



Change:
Code:
#
# Function that stops the daemon/service
#
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
   start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME

to this:

Code:
#
# Function that stops the daemon/service
#
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
   $DAEMON stop
   start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME


to implement BioMike's suggestion. See the '$DAEMON stop' line. This will tell bitcoin (via rpc) to stop running.


I also ended up changing this:

Code:
NAME=/usr/local/bin/bitcoind
DAEMON=$NAME

to:

Code:
NAME=bitcoind
DAEMON=/usr/local/sbin/bitcoind
DAEMON_ARGS="-daemon"

To get things working better for me.  My install of ubuntu wouldn't support long NAMEs.  Also when I issued a 'start' command the process would keep running in the foreground, so I added the '-daemon' argument to stop that from happening.

MPW
newbie
Activity: 1
Merit: 0
June 11, 2011, 02:56:45 AM
#7
Stopping bitcoind is done by sending a signal to the daemon. I don't know how good bitcoind tries to close its databases, but I've had once a database corruption when killing the daemon. On the bitcoin chat people advised me to to send the rpc stop command to stop it (and call start-stop-daemon later again to clean it up), checking the debug log can give some information if it is closing right (flushing the database files). Best would be that bitcoind closes itself properly after receiving a kill signal.

How can I implement this to the skript?
newbie
Activity: 36
Merit: 0
May 27, 2011, 08:36:23 AM
#6
For some reason, for me, this script on Ubuntu 11.04 cause Apache on the same machine not to start after reboot.
Any idea why?

The solution I found was to run
 sudo update-rc.d bitcoind defaults 92 8
If you already used the update-rc.d command without the numbers, you can undo it with
 sudo update-rc.d -f bitcoind remove

The 92 and 8 numbers where found by looking for what numbers Apache got for Kill and Start in /etc/rc0.d and /etc/rc2.d (was 91 and 9)
adv
full member
Activity: 168
Merit: 100
February 21, 2011, 11:34:12 PM
#5
Hey, I've created startup script for Ubuntu/Debian and want to share it here
Nice.
I think better move USER and GROUP to /etc/default/bitcoind. Also add RUN_ON_STARTUP option to /etc/default/bitcoind.
And contact to Jonas Smedegaard <[email protected]>, for include your script to official package.
newbie
Activity: 47
Merit: 0
February 21, 2011, 10:15:06 AM
#4
Thanks for this script, very useful.

I only changed two things (without knowing much about shell programming):

Quote
NAME=bitcoind
DAEMON=/usr/local/bin/$NAME

Where /usr/local/bin/ has to ba adapted to the proper path. I used some bitcoin ubuntu package, so for me it was actually /usr/bin/

It seems to make more sense to me like that, as $NAME is referenced everywhere in the script. Not sure if it runs properly in the original version? IN any case, it seems to work for me now.
hero member
Activity: 574
Merit: 513
September 03, 2010, 02:39:25 AM
#3
bitcoin doesn't close itself properly when receiving a kill signal?  O_O
legendary
Activity: 1658
Merit: 1001
September 03, 2010, 12:22:51 AM
#2
I've also made an start/stop script and I see 2 problems that I also encountered and was challenging to find a good solution to overcome them:

1) Your bitcoind runs as root. A possible bug that would cause some attacker to run his/her own commands through bitcoin gets root access. Ugh... it does change user
2) Stopping bitcoind is done by sending a signal to the daemon. I don't know how good bitcoind tries to close its databases, but I've had once a database corruption when killing the daemon. On the bitcoin chat people advised me to to send the rpc stop command to stop it (and call start-stop-daemon later again to clean it up), checking the debug log can give some information if it is closing right (flushing the database files). Best would be that bitcoind closes itself properly after receiving a kill signal.
hero member
Activity: 546
Merit: 500
September 01, 2010, 11:11:16 AM
#1
Hey, I've created startup script for Ubuntu/Debian and want to share it here

Update this code with correct path to bitcoind executable in NAME and correct user and group in CHUID

Save this code to /etc/init.d/bitcoind

Then run following commands:
$ sudo chmod +x /etc/init.d/bitcoind
$ sudo update-rc.d bitcoind defaults

That's it. Now it will start with your server start.


#! /bin/sh
### BEGIN INIT INFO
# Provides:          bitcoind
# Required-Start:    $remote_fs
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: bitcoind daemon startup script
# Description:       bitcoind daemon startup script
### END INIT INFO

# Author: Pavel A. Karoukin <[email protected]>
#

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="BitCoin Daemon"
NAME=/usr/local/bin/bitcoind
DAEMON=$NAME
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
CHUID=USER:GROUP

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
   # Return
   #   0 if daemon has been started
   #   1 if daemon was already running
   #   2 if daemon could not be started
   start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
      || return 1
   start-stop-daemon --start --quiet --chuid $CHUID --pidfile $PIDFILE --exec $DAEMON -- \
      $DAEMON_ARGS \
      || return 2
}

#
# Function that stops the daemon/service
#
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
   start-stop-daemon --stop --quiet --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"
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
   #
   # If the daemon can reload its configuration without
   # restarting (for example, when it is sent a SIGHUP),
   # then implement that here.
   #
   start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
   return 0
}

case "$1" in
  start)
   [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
   do_start
   case "$?" in
      0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
      2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
   esac
   ;;
  stop)
   [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
   do_stop
   case "$?" in
      0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
      2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
   esac
   ;;
  #reload|force-reload)
   #
   # If do_reload() is not implemented then leave this commented out
   # and leave 'force-reload' as an alias for 'restart'.
   #
   #log_daemon_msg "Reloading $DESC" "$NAME"
   #do_reload
   #log_end_msg $?
   #;;
  restart|force-reload)
   #
   # If the "reload" option is implemented then remove the
   # 'force-reload' alias
   #
   log_daemon_msg "Restarting $DESC" "$NAME"
   do_stop
   case "$?" in
     0|1)
      do_start
      case "$?" in
         0) log_end_msg 0 ;;
         1) log_end_msg 1 ;; # Old process is still running
         *) log_end_msg 1 ;; # Failed to start
      esac
      ;;
     *)
        # Failed to stop
      log_end_msg 1
      ;;
   esac
   ;;
  *)
   #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
   echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
   exit 3
   ;;
esac

:
Jump to: