Bitcoin Forum

Bitcoin => Bitcoin Technical Support => Topic started by: Reynaldo on February 07, 2015, 07:37:38 PM



Title: Systemd, bitcoind and bitcoin node
Post by: Reynaldo on February 07, 2015, 07:37:38 PM
I'm running a bitcoin node on my raspberry pi b+, the thing is that the daemon sometimes just die and i need to restart it
is there any way to check for it using systemd?

the process bitcoind starts at boot with this systemd configuration as bitcoind.service

Code:
[Unit]
Description=Bitcoin daemon service
Wants=network-online.target
After=network-online.target

[Service]
Type=forking
User=HEREGOESYOURUSER
ExecStart=/usr/bin/bitcoind

[Install]
WantedBy=multi-user.target


can anyone post their systemd files so i can take a look?

edit: configuration that worked

Code:
[Unit]
Description=Bitcoin daemon service
Wants=network.target

[Service]
Type=forking
User=HEREGOESYOURUSER
ExecStart=/usr/bin/bitcoind --daemon
Restart=on-failure

[Install]
WantedBy=multi-user.target


Title: Re: Systemd, bitcoind and bitcoin node
Post by: Newar on February 08, 2015, 05:17:28 AM

Didn't realise systemd is already available in Raspian?

My quick and dirty hack: A crontab that starts the process every x Minutes. If bitcoind already runs it will do nothing, but a log entry. Another crontab takes care removing that log file every so often.


Title: Re: Systemd, bitcoind and bitcoin node
Post by: Bitsky on February 08, 2015, 09:06:48 AM
Try the following in your "[Service]" section:
Code:
RestartSec=60
Restart=on-failure


Title: Re: Systemd, bitcoind and bitcoin node
Post by: Reynaldo on February 10, 2015, 01:36:15 PM

Didn't realise systemd is already available in Raspian?

My quick and dirty hack: A crontab that starts the process every x Minutes. If bitcoind already runs it will do nothing, but a log entry. Another crontab takes care removing that log file every so often.

Not using raspbian, using arch linux arm; I added the Restart=on-failure and will try to add the RestartSec=60; I've fixed it i think, ill update this post with the configuration later :), thanks Bitsky

Btw can you give me the command for the crontab? i've never used it.


Title: Re: Systemd, bitcoind and bitcoin node
Post by: Newar on February 10, 2015, 02:35:36 PM
[...]
Btw can you give me the command for the crontab? i've never used it.

As the user who you usually use to run bitcoind, do:
Code:
crontab -e

Add the following lines:
Code:
@reboot /path/to/your/bitcoind
*/30 * * * * /path/to/your/bitcoind
@weekly rm -rf /var/mail/root

The first line starts bitcoind at boot time, the second one starts it every 30 minutes again. In my system this causes a message to be sent to root informing that bitcoind is already running (if it is), so I added the third line to it to save a bit of space. Note that I solely run this system as a node, if you use yours for other stuff your /var/mail/root might be useful for other things and you wouldn't want it deleted once a week. Like I said, it's not the prettiest of hacks, but works fine in this case.


Title: Re: Systemd, bitcoind and bitcoin node
Post by: Bitsky on February 10, 2015, 06:13:55 PM
Instead of dealing with excess messages, you could write two wrappers. One which starts bitcoind and puts the pid into a file (unless bitcoind already places a pidfile in /var/run/), and a second one which sends a kill -0 to the pid every 5 min and restart if it's gone. That also has the benefit that you don't delete maybe important mails.


Title: Re: Systemd, bitcoind and bitcoin node
Post by: Reynaldo on February 10, 2015, 06:23:33 PM
Instead of dealing with excess messages, you could write two wrappers. One which starts bitcoind and puts the pid into a file (unless bitcoind already places a pidfile in /var/run/), and a second one which sends a kill -0 to the pid every 5 min and restart if it's gone. That also has the benefit that you don't delete maybe important mails.

Sorry I've not updated, yesterday had a rough evening, will try to do it tonight. Killing using the pid its not recommended because you might kill someone that you do not want to, it will depend on Type=xx on the systemd file or if you do not use systemd it will relie on how you started it at startup. For the pid i think its safe to use "pidof bitcoind" to get the pid. Thanks again Bitsky and Newar


Title: Re: Systemd, bitcoind and bitcoin node
Post by: Bitsky on February 10, 2015, 10:16:02 PM
Sorry I've not updated, yesterday had a rough evening, will try to do it tonight. Killing using the pid its not recommended because you might kill someone that you do not want to, it will depend on Type=xx on the systemd file or if you do not use systemd it will relie on how you started it at startup. For the pid i think its safe to use "pidof bitcoind" to get the pid. Thanks again Bitsky and Newar
Using -0 will not kill anything. It just does a check on the process behind the pid. Based on the retval you can then determine what to do.


Title: Re: Systemd, bitcoind and bitcoin node
Post by: Reynaldo on February 16, 2015, 09:37:49 PM
Code:
[Unit]
Description=Bitcoin Daemon
Conflicts=bitcoin-reindex.service
After=network.target

[Service]
User=bitcoin
Group=bitcoin
ExecStart=/usr/bin/bitcoind -daemon=0                       \
                            -conf=/etc/bitcoin/bitcoin.conf \
                            -datadir=/srv/bitcoin           \
                            -pid=/run/bitcoind.pid
ExecReload=/usr/bin/kill -HUP $MAINPID
ExecStop=/usr/bin/bitcoind stop

[Install]
WantedBy=multi-user.target

this is the new systemd file that comes with the new 0.10 release. I suppose that to change my configuration file should be as easy as change -conf=/home/user/.bitcoin/bitcoin.conf
but for datadir how should i list it?