Bitcoin Forum
May 22, 2024, 07:08:09 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: [GUIDE] Configure your miner software as a service under daemontools  (Read 4546 times)
edonkey (OP)
Legendary
*
Offline Offline

Activity: 1150
Merit: 1004



View Profile
April 19, 2014, 05:41:57 PM
Last edit: August 13, 2016, 07:49:44 PM by edonkey
 #1

I run my rigs completely headless. I want the miner software to automatically start at reboot or when it crashes. I also want a few days worth of logs for troubleshooting, but I don't want the logs to chew up too much disk space and I don't want to wear out the SD card in my Raspberry Pi.

There are a number of ways that people autostart their miner software under Linux. But some of the techniques are really not very clean. I'm not interested in running the miner under "screen" and having no reasonable log history. When I want to check on my miners, I prefer to a web front end or a script that calls the miner software's JSON API.

So what I've done is use daemontools from D. J. Bernstein to manage the mining software. It's a simple and awesome set of tools that reminds me of "launchd" for the Mac, but better.

If anyone is interested in going this route, here's how to setup cgminer (although any mining software should work) under daemontools.


Install daemontools

Code:
sudo apt-get install daemontools-run daemontools

Create the directories for the run and log scripts:

Code:
sudo mkdir /etc/service/cgminer
sudo mkdir /etc/service/cgminer/log


Add the service scripts

Put the following script in a file called "run" in the /etc/service/cgminer directory:

Code:
#!/bin/sh
# cgminer/run
# run script for cgminer daemon

# Minimal environment
PATH=/usr/local/bin:/usr/bin:/bin:/

# Redirect stderr to std out so that our log will pick it up
exec 2>&1
echo "*** Starting service cgminer..."

# Exec cgminer, which preserves the same process. Change the path and options as needed.
# Note that you have to run cgminer in text-only, non-terminal mode. I opted to force that
# option here rather than rely on it being set correctly in the conf file.
exec /usr/local/bin/cgminer --config /home/pi/config/cgminer.conf.ltc --text-only

Note that you may have to change the path to the cgminer executable and your conf file above.

By default daemontools will run services as root. This works well for me because I found that cgminer seems to need root permissions to access the USB devices anyway. But if you prefer to run your miner software as another user, it's easy to do. See the "More information" section at the end of this post for links to daemontools documentation.

If you want logging enabled, then you need to put this script in a file called "run" in the /etc/service/cgminer/log directory:

Code:
#!/bin/sh
# cgminer/log
# log script for cgminer daemon

# Minimal environment
PATH=/usr/local/bin:/usr/bin:/bin:/

# Define the path to the log folder
logFolder=/var/log/cgminer

# Make a folder fo the cgminer log files
mkdir -p ${logFolder}
chmod 755 ${logFolder}

# Set up multilog to handle logging the output from cgminer. It will rotate after 10 meg and
# keep two historical copies in addition to the current one.
exec multilog s10485760 n3 ${logFolder}

The above script will put the log files in the /var/log/cgminer directory. The current log file will be called "current". The parameters to multilog above indicate that it should limit the log file size to 10 meg, and that it should keep the most recent 2 log files plus the current one. This way old log data is deleted and you won't run out of disk space.

You have to also make sure that both scripts are executable:

Code:
sudo chmod 755 /etc/service/cgminer/log/run
sudo chmod 755 /etc/service/cgminer/run

If you've done everything right, then cgminer should be running. If you reboot, cgminer will be auto started. If cgminer crashes, then daemontools will restart it automatically.


Check the service

You can use the following command to check on the status of cgminer running under daemontools:

Code:
sudo svstat /etc/service/cgminer

And you can see what's happening in the log with a tail command like this:

Code:
tail -500f /var/log/cgminer/current

If you need to stop cgminer, maybe because you want to change the config file or other maintenance, this command will stop the cgminer service:

Code:
sudo svc -d /etc/service/cgminer

This command will start the service back up again:

Code:
sudo svc -u /etc/service/cgminer


Notes on logging

A couple of thoughts regarding logging. First, I'm not sure how safe it is to run a Rasperry Pi from an SD card with lots of log churn. I'm not an expert on flash technology, but I was concerned that at some point lots of log writes might wear out the flash. Again, I must stress that I don't know if this is a real problem or one that I've imagined.

Originally I thought that I'd have logging on only while my rig was new and that I'd disable it later to conserve flash write cycles. But after running two rigs for over a month, I came to the conclusion that logging is essential. Problems crop up with my rigs, and access to historical logs are the only way to figure out what happened.

Second, when I first set up logging, I could not get it to work. Apparently there's an issue with daemontools where if you create the service "run" script first, then you create the "log/run" script, the daemontools service scanner doesn't see the change and does not enable logging.

To work around this, I rebooted my system after configuring logging. After that, logging worked like a charm. There's probably a more elegant way to work around this issue, but I didn't look into it further.


Using ramlog

Since I decided that logging was essential, but I didn't want to burn up my flash, I decided to use a package called ramlog. This useful package keeps the log files in ram, committing them to disk only on reboot. This should save on flash wear and tear. Instructions for installing ramlog follow here.

EDIT: If you're using Raspbian Jessie, then don't install ramlog. Use log2ram instead. See my post below for details.

First, you need to figure out how big to make the ramlog partition.

So how big is my log folder now?

Code:
sudo du -sh /var/log
9.9M /var/log

How much of that is the miner log?

Code:
sudo du -sh /var/log/cgminer
5.9M /var/log/cgminer

So the regular log files amount to about 4M on my Pi. I've set the miner logging to only keep 3 log files at a time and limit them to 10 meg. I think that means it can grow to 30 meg (including the "current" log).

How much ram do I have left?

Code:
free -m

             total       used       free     shared    buffers     cached
Mem:           437        180        256          0         15         82

But I've seen it a lot lower (like 160M).

So I think 50 meg for the ramlog is probably enough and won't impact the system too bad. Once I knew how big to make my ramlog mount point, I followed these ramlog installation instructions:

https://raw.github.com/swirepe/personalscripts/master/pi/setup-ramlog.sh

In case the above URL goes away, here's the steps I took:

Code:
sudo apt-get install lsof
mkdir ~/packages
cd ~/packages/
wget https://raw.github.com/swirepe/personalscripts/master/pi/ramlog_2.0.0_all.deb
sudo dpkg -i ramlog_2.0.0_all.deb

echo "TMPFS_RAMFS_SIZE=50m" | sudo tee /etc/default/ramlog

sudo reboot

So now all log files are written to a RAM disk based mount point and have no ongoing impact on the flash. When the system is rebooted, the log files are committed to flash, and then read out on the other side, so they aren't lost. But that's a lot less wear and tear than continuously writing log file data.


Shortcuts for convenience

It's nice when I log in via ssh to have a simple set of commands to check on the miner software status, start/stop the miner, etc. Rather than try to remember the commands, I've made a few simple shortcuts that I've added to my .profile, plus help text that's displayed at log in time. This is of course a completely optional step, but I find it helpful.

Here's the script code to add to your .profile file:

Code:
# Miner system shortcuts
alias miner_shutdown='sudo shutdown now'
alias miner_reboot='sudo reboot'

# Shortcuts for miner service
export MINER_SERVICE_PATH="/etc/service/cgminer"
alias miner_stat='sudo svstat ${MINER_SERVICE_PATH}'
alias miner_start='sudo svc -u ${MINER_SERVICE_PATH}'
alias miner_stop='sudo svc -d ${MINER_SERVICE_PATH}'
alias miner_restart='miner_stop; miner_start'

# Miner log shortcuts
export MINER_SERVICE_LOG_PATH="${MINER_SERVICE_PATH}/log"
export MINER_SERVICE_LOG_HIDDEN_PATH="${MINER_SERVICE_LOG_PATH}_hide"
alias miner_log_stat='sudo svstat ${MINER_SERVICE_LOG_PATH}'
alias miner_log_start='sudo svc -u ${MINER_SERVICE_LOG_PATH}'
alias miner_log_stop='sudo svc -d ${MINER_SERVICE_LOG_PATH}'
alias miner_log_tail='tail -500f /var/log/cgminer/current'

# Misc miner shortcuts
alias miner_list='sudo cgminer -n --usb-devs'

# Show some useful commands
miner_help()
{
echo "Here are some helpful mining commands:"
echo
echo "  miner_shutdown    - Shut down the system."
echo "  miner_reboot      - Reboot down the system."
echo
echo "  miner_stat        - Shows how long the cgminer service has been running."
echo "  miner_stop        - Stops cgminer service."
echo "  miner_start       - Starts cgminer service."
echo "  miner_restart     - Restarts cgminer service."
echo
echo "  miner_log_tail    - Tails the current mining log."
echo
    echo "  miner_list        - Asks the miner app to list known devices."
    echo
}

# Display a welcome message
miner_welcome()
{
echo
echo "Welcome to $HOSTNAME!"
echo
miner_help
}

# Show the welcome when logging in
miner_welcome

So if something is wrong, I can ssh in and look at the current log via miner_log_tail. If I've changed my config and I want to restart the miner service, I use miner_restart (which just stops and starts the service).


More information

Lastly, if you're interested in more information regarding daemontools, here are some links that I used to get my feet wet:

http://lgallardo.com/en/2013/05/06/daemontools-o-como-relanzar-un-proceso-si-muere/
http://blog.teksol.info/pages/daemontools/tutorial
http://thedjbway.b0llix.net/daemontools/overview.html

Hopefully someone finds this info helpful.

Was I helpful?   BTC: 3G1Ubof5u8K9iJkM8We2f3amYZgGVdvpHr
cesarm
Member
**
Offline Offline

Activity: 92
Merit: 10


View Profile
November 08, 2014, 06:54:49 PM
 #2

Thank, great guide.
edonkey (OP)
Legendary
*
Offline Offline

Activity: 1150
Merit: 1004



View Profile
August 13, 2016, 07:47:40 PM
 #3

I was setting up a new RPi today with the latest Raspbian Jessie Lite. I was thinking about installing ramlog but I noticed posts from people that had problems with using it on Jessie.

So instead I installed this alternative RAM disk log solution:

https://github.com/azlux/log2ram

It's a very simple install and seems to work well.

Was I helpful?   BTC: 3G1Ubof5u8K9iJkM8We2f3amYZgGVdvpHr
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!