21

I've got a bunch of other services set up to boot automatically when my Ubuntu server starts (services like wicd-daemon and VPN), but I can't figure out how to set up the bitcoin daemon (bitcoind) to start automatically when the system boots up. (This is on a headless system with no GUI or attached monitor.)

I'm trying to learn how to integrate bitcoin with Python so I can write bitcoin-enabled services, but I want the system to automatically start bitcoind when the system boots so that I don't have to do it manually. (It's a pain in the butt to have to download a whole bunch of block-chain at once. It eats away at uptime.)

If this question has already been asked, please direct me to the answer. I searched but couldn't find it. Otherwise, please help me figure this out!

Thanks so much.

1

6 Answers 6

16

EDIT: Caveat emptor on Ubuntu distributions from 14.10 and forward - init was chosen over upstart and is being phased in the future. I don't know when that will be done nor what impact it will have on upstart scripts.

Extract from the page RentFree refers to; note that it assumes that you have created a user called bitcoinuser for the sake of security:

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:

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=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

Though one user suggests the following correction to the configuration:

home=/home/$user
cmd=/usr/bin/bitcoind
pidfile=$home/.bitcoin/bitcoind.pid

and another suggests setting nice for the process so it doesn't hog the CPU

exec start-stop-daemon --start -c $user --chdir $home --pidfile $pidfile --startas $cmd -b --nicelevel 15 -m
6
  • Though this works, the upstart log shows some lines like this: /proc/self/fd/9: 9: /proc/self/fd/9: [[: not found. What's that?
    – kaoD
    Commented Jan 4, 2014 at 13:52
  • 2
    If you're on Ubuntu, try pastebinit -i /etc/init/bitcoind.conf, which will give output a URL with the configuration copied to a publicly accessible page. Double check the URL to make sure that nothing private is in there, then paste the URL here.
    – Jan
    Commented Jan 4, 2014 at 14:31
  • 1
    Woah, didn't know pastebinit did exist, cool! Here it goes: paste.ubuntu.com/6691665 but it's pretty much copy and paste from here.
    – kaoD
    Commented Jan 4, 2014 at 16:21
  • I don't really see anything that should cause problems, but I haven't spent much time on init.d/upstart - here's mine for reference, where I've set it to log and to be more explicit in its output. Note that I use bitcoinuser as the dedicated user. An aside about pastebinit: You can also pipe to it for command output - more info here.
    – Jan
    Commented Jan 4, 2014 at 16:45
  • 1
    This answer is almost correct. You should change expect daemon to expect fork and oom never to oom score -500. For more details see: stackoverflow.com/a/25731881/1157309
    – c.hill
    Commented Sep 8, 2014 at 19:57
12

There's an upstart script for Ubuntu in the Bitcoin Core source tree. Using that is the most correct way.

However, I just login as the user account I want to run Bitcoin Core daemon, start a terminal (if I'm in the GUI), and run the following command to edit my crontab:

crontab -e

Then I add the following line:

@reboot bitcoind -daemon

Save the file and exit. Now every time the computer boots up, Bitcoin Core daemon will be started. On shutdown, init/upstart/systemd/whatever will send it a SIGTERM signal, which bitcoind will catch and interpret as the command to start a safe shutdown.

2
  • 1
    but upstart is not used by Ubuntu anymore, right?
    – knocte
    Commented Dec 22, 2016 at 8:22
  • 1
    yes what does that mean?
    – tofutim
    Commented Mar 11, 2018 at 2:24
3

I finally end up with this setting for my raspberry

nano /etc/systemd/system/bitcoind.service

content of the file bitcoind.service

[Unit]
Description=bitcoin
After=network.target

[Service]
Type=forking
User=pi
Group=pi

Environment=BITCOIN_PID=/home/pi/.bitcoin/bitcoin.pid
Environment=BITCOIN_HOME=/home/pi/.bitcoin

ExecStart=/usr/local/bin/bitcoind
ExecStop=/bin/kill -15 $MAINPID

[Install]
WantedBy=multi-user.target

Reload systemctl daemon

systemctl daemon-reload

Enabled new bitcoind service

systemctl enable bitcoind

Commands to start or stop the service

systemctl stop bitcoind
systemctl start bitcoind

Show service status

systemctl status bitcoind.service  

More info in https://github.com/bitcoin/bitcoin/tree/master/contrib/init

thanks for the help Janus Troelsen

Update

Don't put arguments since I have considered that there is a configuration in

/home/pi/.bitcoin/bitcoin.conf
3
  • 1
    nice one, but you missed a d: /etc/systemd/system/bitcoind.service.
    – bomben
    Commented Aug 28, 2019 at 10:42
  • All good except you want Type=simple
    – tread
    Commented Feb 11, 2021 at 7:27
  • @tread In Dogecoin, I had trouble getting Type=simple to work. I kept getting dogecoind.service: Deactivated Successfully. Only after I set it to Type=forking was I able to get it working.
    – david2278
    Commented Dec 28, 2023 at 18:58
2

I've never run it before. You said that bitcoind is a daemon, so here how to configure a daemon to run at startup :

Normally a daemon has its init script in /etc/init.d/ directory.

sudo /etc/init.d/bitcoind start

If the daemon needs to be initialized at startup, then it will be linked in /etc/rc0.d/ or /etc/rc1.d/ or ... /etc/rc6.d/ ... etc

0 -> 6 are runlevels :

0 - Halt
1 - Single User Mode
2 - Multiuser
3 - Full Multiuser Mode
4 - unused
5 - Full Multiuser Mode with x11
6 - Reboot

you can also add the script that your daemon runs in

/etc/rc.local

If it is a python script you can write to that file :

python /directory/to/init/script/bitcoind.py

Another alternative is to execute update-rc.d :

sudo update-rc.d bitcoind defaults

defaults , will tells update-rc.d to run a default configuration, when it comes to runlevel . You can change your runlevel, this is an exampl :

update-rc.d bitcoind start 20 2 3 4 5 . stop 20 0 1 6

run man update-rc.d , there are other examples .

To remove it from startup, run :

sudo update-rc.d bitcoind remove
2
  • I think i removed bitcoind from my startup with your last command. Now it seems that I have troubles to activate it again. My bitcoind had a special working directory. I suspect that the startup skript contained this directory. Did I remove the file physically from /etc/rc.local or /etc/init with this command? Because I dont have a file there any more which is related to bitcoind.
    – bomben
    Commented Aug 28, 2019 at 8:19
  • Ah, I have a bitcoin.service in /etc/systemd/system/ which contains the right path.
    – bomben
    Commented Aug 28, 2019 at 8:24
1

This seems to be your answer: https://bitcointalk.org/index.php?topic=25518.0

-3

I've developed a script that may help you install AND run Bitcoin's CLI daemon at the same time.

sudo apt-get install software-properties-common
<your password>
y
sudo apt-get update
sudo apt-add-repository ppa:bitcoin/bitcoin

sudo apt-get update
sudo apt-get upgrade
y
sudo apt-get install bitcoind
y
bitcoind -daemon
bitcoind -testnet -daemon

*The space between apt-add-repository and update is supposed to be there because of a prompt to press ENTER.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.