Skip to content

Commit 234bfbf

Browse files
committed
Add init scripts and docs for Upstart and OpenRC
1 parent 8b4616b commit 234bfbf

File tree

8 files changed

+289
-51
lines changed

8 files changed

+289
-51
lines changed

contrib/init/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Sample configuration files for:
2+
3+
SystemD: bitcoind.service
4+
Upstart: bitcoind.conf
5+
OpenRC: bitcoind.openrc
6+
bitcoind.openrcconf
7+
8+
have been made available to assist packagers in creating node packages here.
9+
10+
See doc/init.md for more information.

contrib/init/bitcoind.conf

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
description "Bitcoin Core Daemon"
2+
3+
start on runlevel [2345]
4+
stop on starting rc RUNLEVEL=[016]
5+
6+
env BITCOIND_BIN="/usr/bin/bitcoind"
7+
env BITCOIND_USER="bitcoin"
8+
env BITCOIND_GROUP="bitcoin"
9+
env BITCOIND_PIDDIR="/var/run/bitcoind"
10+
# upstart can't handle variables constructed with other variables
11+
env BITCOIND_PIDFILE="/var/run/bitcoind/bitcoind.pid"
12+
env BITCOIND_CONFIGFILE="/etc/bitcoin/bitcoin.conf"
13+
env BITCOIND_DATADIR="/var/lib/bitcoind"
14+
15+
expect fork
16+
17+
respawn
18+
respawn limit 5 120
19+
kill timeout 60
20+
21+
pre-start script
22+
# this will catch non-existent config files
23+
# bitcoind will check and exit with this very warning, but it can do so
24+
# long after forking, leaving upstart to think everything started fine.
25+
# since this is a commonly encountered case on install, just check and
26+
# warn here.
27+
if ! grep -qs '^rpcpassword=' "$BITCOIND_CONFIGFILE" ; then
28+
echo "ERROR: You must set a secure rpcpassword to run bitcoind."
29+
echo "The setting must appear in $BITCOIND_CONFIGFILE"
30+
echo
31+
echo "This password is security critical to securing wallets "
32+
echo "and must not be the same as the rpcuser setting."
33+
echo "You can generate a suitable random password using the following"
34+
echo "command from the shell:"
35+
echo
36+
echo "bash -c 'tr -dc a-zA-Z0-9 < /dev/urandom | head -c32 && echo'"
37+
echo
38+
echo "It is also recommended that you also set alertnotify so you are "
39+
echo "notified of problems:"
40+
echo
41+
echo "ie: alertnotify=echo %%s | mail -s \"Bitcoin Alert\"" \
42+
43+
echo
44+
exit 1
45+
fi
46+
47+
mkdir -p "$BITCOIND_PIDDIR"
48+
chmod 0755 "$BITCOIND_PIDDIR"
49+
chown $BITCOIND_USER:$BITCOIND_GROUP "$BITCOIND_PIDDIR"
50+
chown $BITCOIND_USER:$BITCOIND_GROUP "$BITCOIND_CONFIGFILE"
51+
chmod 0660 "$BITCOIND_CONFIGFILE"
52+
end script
53+
54+
exec start-stop-daemon \
55+
--start \
56+
--pidfile "$BITCOIND_PIDFILE" \
57+
--chuid $BITCOIND_USER:$BITCOIND_GROUP \
58+
--exec "$BITCOIND_BIN" \
59+
-- \
60+
-pid="$BITCOIND_PIDFILE" \
61+
-conf="$BITCOIND_CONFIGFILE" \
62+
-datadir="$BITCOIND_DATADIR" \
63+
-disablewallet \
64+
-daemon
65+

contrib/init/bitcoind.openrc

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/sbin/runscript
2+
3+
# backward compatibility for existing gentoo layout
4+
#
5+
if [ -d "/var/lib/bitcoin/.bitcoin" ]; then
6+
BITCOIND_DEFAULT_DATADIR="/var/lib/bitcoin/.bitcoin"
7+
else
8+
BITCOIND_DEFAULT_DATADIR="/var/lib/bitcoind"
9+
fi
10+
11+
BITCOIND_CONFIGFILE=${BITCOIND_CONFIGFILE:-/etc/bitcoin/bitcoin.conf}
12+
BITCOIND_PIDDIR=${BITCOIND_PIDDIR:-/var/run/bitcoind}
13+
BITCOIND_PIDFILE=${BITCOIND_PIDFILE:-${BITCOIND_PIDDIR}/bitcoind.pid}
14+
BITCOIND_DATADIR=${BITCOIND_DATADIR:-${BITCOIND_DEFAULT_DATADIR}}
15+
BITCOIND_USER=${BITCOIND_USER:-bitcoin}
16+
BITCOIND_GROUP=${BITCOIND_GROUP:-bitcoin}
17+
BITCOIND_BIN=${BITCOIND_BIN:-/usr/bin/bitcoind}
18+
19+
name="Bitcoin Core Daemon"
20+
description="Bitcoin crypto-currency p2p network daemon"
21+
22+
command="/usr/bin/bitcoind"
23+
command_args="-pid=\"${BITCOIND_PIDFILE}\" \
24+
-conf=\"${BITCOIND_CONFIGFILE}\" \
25+
-datadir=\"${BITCOIND_DATADIR}\" \
26+
-daemon \
27+
${BITCOIND_OPTS}"
28+
29+
required_files="${BITCOIND_CONFIGFILE}"
30+
start_stop_daemon_args="-u ${BITCOIND_USER} \
31+
-N ${BITCOIND_NICE:-0} -w 2000"
32+
pidfile="${BITCOIND_PIDFILE}"
33+
retry=60
34+
35+
depend() {
36+
need localmount net
37+
}
38+
39+
# verify
40+
# 1) that the datadir exists and is writable (or create it)
41+
# 2) that a directory for the pid exists and is writable
42+
# 3) ownership and permissions on the config file
43+
start_pre() {
44+
checkpath \
45+
-d \
46+
--mode 0750 \
47+
--owner "${BITCOIND_USER}:${BITCOIND_GROUP}" \
48+
"${BITCOIND_DATADIR}"
49+
50+
checkpath \
51+
-d \
52+
--mode 0755 \
53+
--owner "${BITCOIND_USER}:${BITCOIND_GROUP}" \
54+
"${BITCOIND_PIDDIR}"
55+
56+
checkpath -f \
57+
-o ${BITCOIND_USER}:${BITCOIND_GROUP} \
58+
-m 0660 \
59+
${BITCOIND_CONFIGFILE}
60+
61+
checkconfig || return 1
62+
}
63+
64+
checkconfig()
65+
{
66+
if ! grep -qs '^rpcpassword=' "${BITCOIND_CONFIGFILE}" ; then
67+
eerror ""
68+
eerror "ERROR: You must set a secure rpcpassword to run bitcoind."
69+
eerror "The setting must appear in ${BITCOIND_CONFIGFILE}"
70+
eerror ""
71+
eerror "This password is security critical to securing wallets "
72+
eerror "and must not be the same as the rpcuser setting."
73+
eerror "You can generate a suitable random password using the following"
74+
eerror "command from the shell:"
75+
eerror ""
76+
eerror "bash -c 'tr -dc a-zA-Z0-9 < /dev/urandom | head -c32 && echo'"
77+
eerror ""
78+
eerror "It is also recommended that you also set alertnotify so you are "
79+
eerror "notified of problems:"
80+
eerror ""
81+
eerror "ie: alertnotify=echo %%s | mail -s \"Bitcoin Alert\"" \
82+
83+
eerror ""
84+
return 1
85+
fi
86+
}

contrib/init/bitcoind.openrcconf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# /etc/conf.d/bitcoind: config file for /etc/init.d/bitcoind
2+
3+
# Config file location
4+
#BITCOIND_CONFIGFILE="/etc/bitcoin/bitcoin.conf"
5+
6+
# What directory to write pidfile to? (created and owned by $BITCOIND_USER)
7+
#BITCOIND_PIDDIR="/var/run/bitcoind"
8+
9+
# What filename to give the pidfile
10+
#BITCOIND_PIDFILE="${BITCOIND_PIDDIR}/bitcoind.pid"
11+
12+
# Where to write bitcoind data (be mindful that the blockchain is large)
13+
#BITCOIND_DATADIR="/var/lib/bitcoind"
14+
15+
# User and group to own bitcoind process
16+
#BITCOIND_USER="bitcoin"
17+
#BITCOIND_GROUP="bitcoin"
18+
19+
# Path to bitcoind executable
20+
#BITCOIND_BIN="/usr/bin/bitcoind"
21+
22+
# Nice value to run bitcoind under
23+
#BITCOIND_NICE=0
24+
25+
# Additional options (avoid -conf and -datadir, use flags above)
26+
BITCOIND_OPTS="-disablewallet"
27+

contrib/systemd/bitcoind.service renamed to contrib/init/bitcoind.service

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@ Description=Bitcoin's distributed currency daemon
33
After=network.target
44
55
[Service]
6-
User=bitcoind
7-
Group=bitcoind
6+
User=bitcoin
7+
Group=bitcoin
88
99
Type=forking
1010
PIDFile=/var/lib/bitcoind/bitcoind.pid
11-
ExecStart=/usr/bin/bitcoind -daemon -pid=/var/lib/bitcoind/bitcoind.pid -conf=/etc/bitcoind.conf -datadir=/var/lib/bitcoind
11+
ExecStart=/usr/bin/bitcoind -daemon -pid=/var/lib/bitcoind/bitcoind.pid \
12+
-conf=/etc/bitcoin/bitcoin.conf -datadir=/var/lib/bitcoind -disablewallet
1213
1314
Restart=always
1415
PrivateTmp=true
16+
TimeoutStopSec=60s
17+
TimeoutStartSec=2s
18+
StartLimitInterval=120s
19+
StartLimitBurst=5
1520
1621
[Install]
1722
WantedBy=multi-user.target

doc/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ The Bitcoin repo's [root README](https://github.com/bitcoin/bitcoin/blob/master/
6868
- [Assets Attribution](assets-attribution.md)
6969
- [Files](files.md)
7070
- [Tor Support](tor.md)
71-
- [Systemd](systemd.md)
71+
- [Init Scripts (systemd/upstart/openrc)](init.md)
7272

7373
License
7474
---------------------

doc/init.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
Sample init scripts and service configuration for bitcoind
2+
==========================================================
3+
4+
Sample scripts and configuration files for systemd, Upstart and OpenRC
5+
can be found in the contrib/init folder.
6+
7+
contrib/init/bitcoind.service: systemd service unit configuration
8+
contrib/init/bitcoind.openrc: OpenRC compatible SysV style init script
9+
contrib/init/bitcoind.openrcconf: OpenRC conf.d file
10+
contrib/init/bitcoind.conf: Upstart service configuration file
11+
12+
1. Service User
13+
---------------------------------
14+
15+
All three startup configurations assume the existence of a "bitcoin" user
16+
and group. They must be created before attempting to use these scripts.
17+
18+
2. Configuration
19+
---------------------------------
20+
21+
At a bare minimum, bitcoind requires that the rpcpassword setting be set
22+
when running as a daemon. If the configuration file does not exist or this
23+
setting is not set, bitcoind will shutdown promptly after startup.
24+
25+
This password does not have to be remembered or typed as it is mostly used
26+
as a fixed token that bitcoind and client programs read from the configuration
27+
file, however it is recommended that a strong and secure password be used
28+
as this password is security critical to securing the wallet should the
29+
wallet be enabled.
30+
31+
If bitcoind is run with "-daemon" flag, and no rpcpassword is set, it will
32+
print a randomly generated suitable password to stderr. You can also
33+
generate one from the shell yourself like this:
34+
35+
bash -c 'tr -dc a-zA-Z0-9 < /dev/urandom | head -c32 && echo'
36+
37+
Once you have a password in hand, set rpcpassword= in /etc/bitcoin/bitcoin.conf
38+
39+
For an example configuration file that describes the configuration settings,
40+
see contrib/debian/examples/bitcoin.conf.
41+
42+
3. Paths
43+
---------------------------------
44+
45+
All three configurations assume several paths that might need to be adjusted.
46+
47+
Binary: /usr/bin/bitcoind
48+
Configuration file: /etc/bitcoin/bitcoin.conf
49+
Data directory: /var/lib/bitcoind
50+
PID file: /var/run/bitcoind/bitcoind.pid (OpenRC and Upstart)
51+
/var/lib/bitcoind/bitcoind.pid (systemd)
52+
53+
The configuration file, PID directory (if applicable) and data directory
54+
should all be owned by the bitcoin user and group. It is advised for security
55+
reasons to make the configuration file and data directory only readable by the
56+
bitcoin user and group. Access to bitcoin-cli and other bitcoind rpc clients
57+
can then be controlled by group membership.
58+
59+
4. Installing Service Configuration
60+
-----------------------------------
61+
62+
4a) systemd
63+
64+
Installing this .service file consists on just copying it to
65+
/usr/lib/systemd/system directory, followed by the command
66+
"systemctl daemon-reload" in order to update running systemd configuration.
67+
68+
To test, run "systemctl start bitcoind" and to enable for system startup run
69+
"systemctl enable bitcoind"
70+
71+
4b) OpenRC
72+
73+
Rename bitcoind.openrc to bitcoind and drop it in /etc/init.d. Double
74+
check ownership and permissions and make it executable. Test it with
75+
"/etc/init.d/bitcoind start" and configure it to run on startup with
76+
"rc-update add bitcoind"
77+
78+
4c) Upstart (for Debian/Ubuntu based distributions)
79+
80+
Drop bitcoind.conf in /etc/init. Test by running "service bitcoind start"
81+
it will automatically start on reboot.
82+
83+
NOTE: This script is incompatible with CentOS 5 and Amazon Linux 2014 as they
84+
use old versions of Upstart and do not supply the start-stop-daemon uitility.
85+
86+
5. Auto-respawn
87+
-----------------------------------
88+
89+
Auto respawning is currently only configured for Upstart and systemd.
90+
Reasonable defaults have been chosen but YMMV.
91+
92+

doc/systemd.md

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)