Skip to content

Instantly share code, notes, and snippets.

@davidlj95
Last active March 20, 2018 22:08
Show Gist options
  • Save davidlj95/8f8fec01f6ae05035fa8235ddb15891c to your computer and use it in GitHub Desktop.
Save davidlj95/8f8fec01f6ae05035fa8235ddb15891c to your computer and use it in GitHub Desktop.
bitcoind Systemd Unit service (with templates)
[Unit]
Description=Bitcoin's [%i] distributed currency daemon
After=network.target
[Service]
User=bitcoin-%i
Group=bitcoin-%i
Type=forking
PIDFile=/var/blockchain/bitcoin/bitcoind_%i.pid
ExecStart=/usr/local/bin/bitcoind -daemon -pid=/var/blockchain/bitcoin/bitcoind_%i.pid \
-conf=/etc/bitcoin/%i.conf -datadir=/var/blockchain/bitcoin
# IMPORTANT!: You must include datadir even if it's specified in the configuration
RuntimeDirectory=bitcoind
Restart=on-failure
PrivateTmp=true
ProtectSystem=full
NoNewPrivileges=true
PrivateDevices=true
MemoryDenyWriteExecute=true
TimeoutStopSec=60s
TimeoutStartSec=2s
StartLimitInterval=120s
StartLimitBurst=5
[Install]
WantedBy=multi-user.target
@davidlj95
Copy link
Author

davidlj95 commented Mar 20, 2018

Bitcoin Core's systemd unit

Explanation

This systemd unit file improves the official systemd unit file present in the Bitcoin Core GitHub repository by allowing several daemons of bitcoind run in the same machine, with different configurations (ie: two daemons that sync both mainnet and testnet blockchains).

Because it's a template unit file, you can run more than one daemon with the same configuration, just modifying the necessary fields so that both daemons do different things. In order to do that, you must name each daemon when instancing the template file

Source

Based on official bitcoin/bitcoin GitHub repository init file for systemd

Requirements

Before using this systemd unit, you must have the following requirements:

User and group

Create a user and group for each bitcoin daemon (one user and group per mainnet, another by testnet).

Users and group must be prefixed with the prefix bitcoin-. The name specified after the prefix will be the name of the configuration you are creating for the unit file (ie: bitcoin-mainnet)

Data folders

Create the data folders where the blockchain of each daemon will be stored. Remember to set the permissions properly so that the folder specified in the unit file can be read by the user and group specified in the same unit file.

You can use the variable %i to separate folders for each daemon (template unit instance)

Remember that in Bitcoin Core, the root folder stores the mainnet blockchain and a child directory named testnet3 is created to store the testnet network (so you can have a single directory to contain all bitcoin data)

Configuration files

We use the %i variable so we can specify the configuration file for the daemon dynamically. We store all of them in /etc/bitcoin.

So you can create multiple configuration files there and depending on the instance of the unit file, your daemon will use one or another

Due to a bug, the datafolder is not taken from the configuration file, but from the arguments in the ExecStart directive. You can't neither leave this argument empty in the ExecStart arguments or bitcoind will use the default value.

Usage

In order to use this systemd service unit, you have to make sure the requirements are fulfilled.

Then, copy the file into a systemd units directory, like /etc/systemd/system

Edit the file and make sure all users, groups, folders exist. Finally, reload the daemons from disk so you can control it.
sudo systemctl daemon-reload

To start an instance of the template unit file (ie: named mainnet), use the following command
sudo systemctl start bitcoind@mainnet

Remember always to specify the @instance to specify which instance you're controlling. You can use the rest of systemd commands to to start the daemon at boot (enable), stop (stop), or disable from starting at boot (disable) for example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment