Bitcoin Forum
September 27, 2016, 05:17:30 AM *
News: Latest stable version of Bitcoin Core: 0.13.0 (New!) [Torrent]. Make sure you verify it.
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: Ubuntu/Debian startup script  (Read 19901 times)
hippich
Hero Member
*****
Offline Offline

Activity: 546


View Profile
September 01, 2010, 04:11:16 PM
 #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 <pavel@yepcorp.com>
#

# 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

:

1474953450
Hero Member
*
Offline Offline

Posts: 1474953450

View Profile Personal Message (Offline)

Ignore
1474953450
Reply with quote  #2

1474953450
Report to moderator
1474953450
Hero Member
*
Offline Offline

Posts: 1474953450

View Profile Personal Message (Offline)

Ignore
1474953450
Reply with quote  #2

1474953450
Report to moderator
1474953450
Hero Member
*
Offline Offline

Posts: 1474953450

View Profile Personal Message (Offline)

Ignore
1474953450
Reply with quote  #2

1474953450
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1474953450
Hero Member
*
Offline Offline

Posts: 1474953450

View Profile Personal Message (Offline)

Ignore
1474953450
Reply with quote  #2

1474953450
Report to moderator
1474953450
Hero Member
*
Offline Offline

Posts: 1474953450

View Profile Personal Message (Offline)

Ignore
1474953450
Reply with quote  #2

1474953450
Report to moderator
BioMike
Legendary
*
Offline Offline

Activity: 1218


View Profile
September 03, 2010, 05: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. <edit>Ugh... it does change user</edit>
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.
mizerydearia
Hero Member
*****
Offline Offline

Activity: 574



View Profile
September 03, 2010, 07:39:25 AM
 #3

bitcoin doesn't close itself properly when receiving a kill signal?  O_O
Fractality
Jr. Member
*
Offline Offline

Activity: 44



View Profile WWW
February 21, 2011, 03:15:06 PM
 #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.

1K4noMkNgrPztyqtHCYd2CevsZ72FeYUKF
adv
Full Member
***
Offline Offline

Activity: 169


View Profile
February 22, 2011, 04:34:12 AM
 #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 <dr@jones.dk>, for include your script to official package.

U may thank me here: 14Js1ng1SvYBPgUJnjNAEPYH4d6SHF79UF
udibr
Jr. Member
*
Offline Offline

Activity: 36


View Profile
May 27, 2011, 01:36:23 PM
 #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)
MPW
Newbie
*
Offline Offline

Activity: 1


View Profile
June 11, 2011, 07: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?
btcmonkey
Newbie
*
Offline Offline

Activity: 20


View Profile
June 22, 2011, 03:24:38 PM
 #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.

Chick
Member
**
Offline Offline

Activity: 70


View Profile
July 08, 2011, 05: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"?

phungus
Full Member
***
Offline Offline

Activity: 128


I'm doin' fine on cloud 9


View Profile
July 11, 2011, 12:20:44 AM
 #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 <<EOF | sudo tee -a ~bitcoinuser/.bitcoin/bitcoin.conf
# 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 <<EOF | sudo tee -a /etc/init/bitcoind.conf
# 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!


BitVPS -- Senior System Admin, Co-Owner
http://www.bitvps.com -- Reliable Servers and Support - KVM, OpenVZ & Dedicated
jamesg
VIP
Legendary
*
Offline Offline

Activity: 1330


AKA: gigavps


View Profile
February 06, 2013, 12:45:03 PM
 #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
phungus
Full Member
***
Offline Offline

Activity: 128


I'm doin' fine on cloud 9


View Profile
February 07, 2013, 04:44:02 PM
 #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

BitVPS -- Senior System Admin, Co-Owner
http://www.bitvps.com -- Reliable Servers and Support - KVM, OpenVZ & Dedicated
wumpus
Hero Member
*****
qt
Offline Offline

Activity: 798

No Maps for These Territories


View Profile
May 04, 2014, 11: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

Bitcoin Core developer [PGP] Warning: For most, coin loss is a larger risk than coin theft. A disk can die any time. Regularly back up your wallet through FileBackup Wallet to an external storage or the (encrypted!) cloud. Use a separate offline wallet for storing larger amounts.
jamesg
VIP
Legendary
*
Offline Offline

Activity: 1330


AKA: gigavps


View Profile
February 20, 2015, 11: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
tzpardi
Member
**
Offline Offline

Activity: 67


View Profile
February 25, 2015, 02:54:02 AM
 #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!
Andi123
Newbie
*
Offline Offline

Activity: 1


View Profile
March 20, 2015, 11: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.


Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!