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.
Hi and thanks for the article! I’ll sure try it out.
Could you tell me what everything means in the following line?
screen -S cgminer -X stuff ‘scy’
I found no “-y” switch in the manual of cgminer, and -s means scan-time. There’s something I don’t get.
Also do you know if systemd can detect if cgminer hangs, and restart it?
Hi Marko,
In reference to the line:
screen -S cgminer -X stuff ‘scy’
The ‘-S’ flag identifies the screen session named ‘cgminer’,
the ‘-X’ flag tells screen to send a command to the session, and
the command to send is the screen command ‘stuff’ which tells screen to send the argument ‘scy’ as though I had typed it directly into the cgminer session.
Here’s the reference to stuff in the screen manpage:
So if cgminer is running interactively, and you typed ‘scy’, then
the ‘s’ would cause cgminer to bring up interactive [S]ettings,
the ‘c’ would select [C]gminer Restart,
and the ‘y’ would confirm the restart because cgminer will ask ‘Are you sure?’
When cgminer restarts, it will re-read the configuration file which accomplishes the intent of the reload command for the service. You can find the [S]ettings and [C]gminer restart commands referenced in the ‘WHILE RUNNING’ section of the cgminer readme.
I haven’t had issues with cgminer hanging, and so I’m not sure if systemd would detect that situation – probably not. In my case, cgminer usually terminates if there is a problem, and when that happens, the screen session also terminates, and systemd can detect that and restart it. If I’ve overclocked the GPUs too hard, and they hang the system, then restarting cgminer probably isn’t enough. In that situation I usually have to restart X or even the entire system, but that isn’t usually a problem for me once I have a stable configuration.
Hope this helps,
Bill