Author

Topic: bitcoind upstart configuration - ubuntu (Read 15484 times)

newbie
Activity: 18
Merit: 0
December 01, 2014, 02:12:40 PM
#7
The only script that worked for me is the usual way - in /etc/init.d directory:

################################################################################

#! /bin/sh

user="kostaz";
# peer="Huh";
dir="/home/${user}";
daemon="bitcoind";
pidfile="${dir}/.bitcoin/${daemon}.pid";
daemonfull="/usr/bin/bitcoind";
# args="-daemon -addnode=${peer}";
# args="-daemon";

do_start ()
{
   echo "Starting bitcoin server..."
   echo "start-stop-daemon --start --chuid $user --chdir $dir --pidfile $pidfile --startas $daemonfull"
   # start-stop-daemon --start --chuid $user --chdir $dir --pidfile $pidfile --startas $daemonfull -- $args
   start-stop-daemon --start --chuid $user --chdir $dir --pidfile $pidfile --startas $daemonfull
}

do_stop ()
{
   echo "Stopping bitcoin server..."
   echo "start-stop-daemon --stop --chuid $user --chdir $dir --pidfile $pidfile --startas $daemonfull"
   start-stop-daemon --stop --chuid $user --chdir $dir --pidfile $pidfile --startas $daemonfull
}

do_status ()
{
   echo "Checking status of bitcoin server..."
   echo "start-stop-daemon --status --chuid $user --chdir $dir --pidfile $pidfile --startas $daemonfull"
   start-stop-daemon --status --chuid $user --chdir $dir --pidfile $pidfile --startas $daemonfull
}

case "$1" in
  start|"")
   do_start
   ;;
  stop)
   do_stop
   ;;
  status)
   do_status
   ;;
  restart)
   do_stop
   sleep 10
   do_start
   ;;
  *)
   echo "Wrong arguments"
   exit 3
   ;;
esac

:

################################################################################

After copying the script to /etc/init.d/bitcoin file and making it executable (with chmod +x) you can do:

$ sudo service bitcoin start
$ sudo service bitcoin stop

And by creating soft link to this script at /etc/rc2.d directory bitcoin server is started after the Ubuntu 14.04 boot:

$ ln -s /etc/init.d/bitcoin S93bitcoin

--- Kosta
newbie
Activity: 8
Merit: 0
September 08, 2014, 03:03:33 PM
#6
Hi

I've tried to use this script and it works fine for starting the daemon on system boot, but doesn't help to restart it if process fails, although it should, as it has "respawn" stanza. I've tried numerous ways to do that and sorted a few minor issues on the way, but this is still not working for me. Any ideas how to make it restart automatically with upstart?

regards
Alex

So I finally got things working on an Ubuntu 14.04 server. Here's what the final, working /etc/init/bitcoind.conf looks like:

Code:
description "bitcoind"

start on filesystem
stop on runlevel [!2345]
oom score -500
expect fork
respawn
respawn limit 10 60 # 10 times in 60 seconds

script
user=bitcoind
home=/home/$user
cmd=$home/bin/bitcoind
pidfile=$home/bitcoind.pid
# Don't change anything below here unless you know what you're doing
[[ -e $pidfile && ! -d "/proc/$(cat $pidfile)" ]] && rm $pidfile
[[ -e $pidfile && "$(cat /proc/$(cat $pidfile)/cmdline)" != $cmd* ]] && rm $pidfile
exec start-stop-daemon --start -c $user --chdir $home --pidfile $pidfile -m --startas $cmd
end script

Once you've added/updated your /etc/init/bitcoin.conf file, be sure to run the following:

Code:
initctl reload-configuration

Basically this was just a lot of guess and check to make this finally work. Here's the important bit:

Code:
expect fork

Essentially, this is telling upstart how many times the target process will be forked while starting. If you tell it wrong, it'll hang while starting. Read here for the specifics on this.

You should be able to manually start bitcoind as a service, like this:

Code:
service bitcoind start

Or stop it, like this:

Code:
service bitcoind stop

If you restart your server, the bitcoind service should be started automatically. And, if the bitcoind process is killed or crashes, it will be automatically respawned. You can test that part out on your server by first finding the PID of the bitcoind process:

Code:
ps cax | grep bitcoind

Then, kill the process manually:

Code:
kill -9 PID_OF_BITCOIND

Then, try to get the PID of the bitcoind process again:

Code:
ps cax | grep bitcoind

It should still be running and with a new PID.
newbie
Activity: 25
Merit: 0
June 11, 2014, 08:45:15 AM
#5
Hi

I've tried to use this script and it works fine for starting the daemon on system boot, but doesn't help to restart it if process fails, although it should, as it has "respawn" stanza. I've tried numerous ways to do that and sorted a few minor issues on the way, but this is still not working for me. Any ideas how to make it restart automatically with upstart?

regards
Alex
newbie
Activity: 15
Merit: 0
November 12, 2013, 02:13:42 PM
#4
Thanks for posting this upstart config frozen (it's the first result on Google). If bitcoind is not mission-critical to your server, I highly recommend setting the nice level as well.

Code:
exec start-stop-daemon --start -c $user --chdir $home --pidfile $pidfile --startas $cmd -b --nicelevel 15 -m
member
Activity: 77
Merit: 10
July 01, 2013, 09:49:32 AM
#3
i can't get the bitcoind.pid file to show up what do i do?
newbie
Activity: 36
Merit: 0
June 22, 2013, 03:48:15 PM
#2
Thanks, works like a charm!

Regarding configuration I had to use these values:

Code:
home=/home/$user
cmd=/usr/bin/bitcoind
pidfile=$home/.bitcoin/bitcoind.pid
full member
Activity: 196
Merit: 100
July 02, 2011, 10:12:44 PM
#1
For those running ubuntu, you may be familiar with upstart, a replacement for sysvinit style booting (/etc/init.d/*).

Configuration files for upstart are stored under /etc/init/*.conf. I've written the following one for bitcoind:

Code:
description "bitcoind"

start on filesystem
stop on runlevel [!2345]
oom never
expect daemon
respawn
respawn limit 10 60 # 10 times in 60 seconds

script
user=bitcoinuser
home=/home/$user
cmd=$home/bin/bitcoind
pidfile=$home/bitcoind.pid
# Don't change anything below here unless you know what you're doing
[[ -e $pidfile && ! -d "/proc/$(cat $pidfile)" ]] && rm $pidfile
[[ -e $pidfile && "$(cat /proc/$(cat $pidfile)/cmdline)" != $cmd* ]] && rm $pidfile
exec start-stop-daemon --start -c $user --chdir $home --pidfile $pidfile --startas $cmd -b -m
end script

Put the above configuration in /etc/init/bitcoind.conf, then run sudo initctl reload-configuration. To start bitcoind, run sudo start bitcoind. To stop bitcoind, run sudo stop bitcoind.

You'll probably want to change the user, cmd and pidfile to something that matches your configuration, or you can use the following as a template:

/home/bitcoinuser
/home/bitcoinuser/bitcoind.pid
/home/bitcoinuser/bin/bitcoind

Enjoy
Jump to: