Category Archives: Mining

cgminer2

Bitcoin Mining Client cgminer as a systemd Service

Here’s a short and simple way to configure the popular Bitcoin mining client cgminer as a systemd service.  It’s convenient to run cgminer in a screen window so that I can attach to it remotely to see, tweak, etc.  Running the combination as a systemd service lets me start it automatically, connect to it in progress as needed, even issue a systemd reload command to cause cgminer to restart and re-read it’s configuration file after changes are made.

The good news is that systemd is easy and works well!

Requirements

You should already have a functioning Linux distribution utilizing systemd, the Screen application, and a working implementation of cgminer.  I set this up using a Fedora environment so there may be differences for your distribution.

Create Launch Script

Create a short script with any needed environment variables to launch cgminer and make it executable (chmod +x start-miner).  I used ‘start-miner’ in my miner user’s home directory:

#!/bin/sh
# Simple script to start cgminer
cd /home/miner/bitcoin/cgminer-3.4.3/
DISPLAY=:0 ./cgminer --config miner.conf

Create the Service

I’m using a ‘simple’ type service in systemd, but you can also set it up as a ‘forking’ type service by configuring screen to fork in the background (see comments below).  Setup the cgminer service by creating this file:

/lib/systemd/system/cgminer.service

with the following lines (the lines beginning with ‘#’ are comments for alternatives and are not needed):

[Unit]
Description=cgminer
After=network.target
After=graphical.target

[Service]
#Type=forking
Type=simple
User=miner

#Start:
# screen -dm creates detached session that forks.
#  use this with Type=forking
#ExecStart=/usr/bin/screen -LdmS cgminer /home/miner/start-miner
#
# screen -Dm creates detached session that doesn't fork.
#  use this with Type=simple
ExecStart=/usr/bin/screen -LDmS cgminer /home/miner/start-miner

#Stop:
# tell cgminer to quit (not screen), and screen will exit
ExecStop=/usr/bin/screen -S cgminer -X stuff 'q'

# or tell screen to quit and clobber cgminer - not best choice
#ExecStop=/usr/bin/screen -S cgminer -X quit
#Probably need a kill definition in case the miner is hung

#Reload:
# sending the string 'scy' (settings, restart, yes) to cgminer will 
#  cause a restart which will re-read config file if using one
ExecReload=/usr/bin/screen -S cgminer -X stuff 'scy'

[Install]
WantedBy=multi-user.target

Reload the systemd configuration

Any time you change the service definition, you should issue the following command to reload systemd:

# systemctl daemon-reload

Enable the service

When you enable the service, you should see systemd respond by creating the appropriate symbolic link like below:

# systemctl enable cgminer.service
 ln -s '/lib/systemd/system/cgminer.service' '/etc/systemd/system/multi-user.target.wants/cgminer.service'

Starting/Stopping the service

You can start or stop the service normally:

# systemctl start cgminer.service
# systemctl stop cgminer.service

Reload the service

If you use a config file like in my example above in the start_miner script, then you can make changes to it and tell systemd to reload the service.  This will send cgminer the key sequence telling it to restart which will re-read the config file.  If you are attached to the screen window, you can see the restart happen.

# systemctl reload cgminer.service

Status Command

# systemctl status cgminer.service
cgminer.service - cgminer
          Loaded: loaded (/lib/systemd/system/cgminer.service)
          Active: active (running) since Wed, 09 Oct 2013 17:17:53 -0400; 13min ago
        Main PID: 10101 (screen)
          CGroup: name=systemd:/system/cgminer.service
                   10101 /usr/bin/SCREEN -LDmS cgminer /home/miner/start-miner
                   10103 /bin/sh /home/miner/start-miner
                   10104 ./cgminer --config miner.conf

That’s it!  There’s probably much that could be done to make this better, but it is working for me.  Comments or suggestions are always welcome.