Bitcoin Forum
November 09, 2024, 04:49:41 AM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: bitcoind upstart configuration - ubuntu  (Read 15479 times)
frozen (OP)
Full Member
***
Offline Offline

Activity: 196
Merit: 100



View Profile
July 03, 2011, 03:12:44 AM
 #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

jorijnsmit
Newbie
*
Offline Offline

Activity: 36
Merit: 0



View Profile
June 22, 2013, 08: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
cosix
Member
**
Offline Offline

Activity: 77
Merit: 10


View Profile
July 01, 2013, 02:49:32 PM
 #3

i can't get the bitcoind.pid file to show up what do i do?
happyface
Newbie
*
Offline Offline

Activity: 15
Merit: 0


View Profile
November 12, 2013, 07: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
alexykot
Newbie
*
Offline Offline

Activity: 25
Merit: 0


View Profile
June 11, 2014, 01:45:15 PM
 #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
chill1
Newbie
*
Offline Offline

Activity: 8
Merit: 0


View Profile WWW
September 08, 2014, 08: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.
zertsekel
Newbie
*
Offline Offline

Activity: 18
Merit: 0


View Profile
December 01, 2014, 07: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
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!