Title: [GUIDE] Configure your miner software as a service under daemontools Post by: edonkey on April 19, 2014, 05:41:57 PM 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 Add the service scripts Put the following script in a file called "run" in the /etc/service/cgminer directory: Code: #!/bin/sh 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 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 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 How much of that is the miner log? Code: sudo du -sh /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 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 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 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. Title: Re: [GUIDE] Configure your miner software as a service under daemontools Post by: cesarm on November 08, 2014, 06:54:49 PM Thank, great guide.
Title: Re: [GUIDE] Configure your miner software as a service under daemontools Post by: edonkey on August 13, 2016, 07:47:40 PM 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. |