NEW AND IMPROVED METHOD!So while my big hammer method above worked it felt too dirty & dumb. Here's a smarter "2.0" method that reads in the speed via RPC & only stops bfgminer if the speed falls below a certain level.
nano run.sh
#!/bin/sh
while true
do
cd /home/pi/moonlander2/bfgminer_5.4.2-futurebit2_linux_armv6/
./bfgminer --scrypt -o stratum+tcp://POOL:PORT -u USERNAME -p x -S ALL --api-listen
echo "Sleeping 5 seconds.. Press Ctrl-C to stop bfgminer restart."
sleep 5s
done
NOTE: This run.sh is same as previous method
except --api-listen is added to enable RPC API for localhost reading of mining stats. Be sure to set the correct path to your bfgminer, your own POOL, PORT & USERNAME etc. Set your own command line options like clock speeds etc as needed
Make it executable:
sudo chmod +x run.sh
We also need a new script:
nano monitor.sh
#!/bin/sh
MINSPEED=10
MINWAIT=180
cd /home/pi/moonlander2/bfgminer_5.4.2-futurebit2_linux_armv6/
RPCDATA=$(./bfgminer-rpc)
SPEEDTXT=$(echo "$RPCDATA" | grep '\[MHS 20s\]' | sed -e 's/^[[:space:]]*//')
ELAPSEDTXT=$(echo "$RPCDATA" | grep '\[Elapsed\]' | sed -e 's/^[[:space:]]*//')
SPEEDFLOAT=${SPEEDTXT##*>}
SPEED=${SPEEDFLOAT%.*}
ELAPSED=${ELAPSEDTXT##*>}
if [ $ELAPSED -gt $MINWAIT ] && [ "$SPEED" -lt "$MINSPEED" ]; then
echo "Slow speed detected.. Killing bfgminer!"
/usr/bin/pkill -f bfgminer
fi
echo "ELAPSED:" $ELAPSED "SPEED:" $SPEED
NOTE: Be sure to set MINSPEED, MINWAIT & path to where bfgminer-rpc is (the cd line)
Make it executable:
sudo chmod +x monitor.sh
sudo crontab -e
*/5 * * * * /home/pi/monitor.sh
(Don't forget sudo so you're editing root's crontab. This runs the monitor every 5 minutes, adjust accordingly. Be sure to set path to your script as well. I just put mine in my pi home directory)
So what the heck does this do? Well like before I'm running bfgminer a screen session, in a loop, so that if told to exit it will just run again after a 5 second delay to give you time to hit Ctrl-C to terminate the script. But now instead of blindly killing off bgminer every hour, I'm running my monitor script every 5 minutes. The monitor script calls bfgminer-rpc to get the stats (remember you must enable RPC with --api-listen option unlike the earlier method above), parses that output for the 20 second average speed and how much seconds have elapsed since bfgminer started. If the speed is less than MINSPEED (default is 10 MHS since I have 4 stock moonlanders running. Obviously adjust this to suit your speed. Go a little under the average you see in bfgminer though to give it a little wiggle room. I see 12-13 on my 4 so 10 seems like a pretty safe level to detected 1 or more that are stuck) and elapsed time is at least MINWAIT (default is 180 seconds or 3 minutes to give the miners time to settle in & not try to restart bfgminer while it's ramping up. Adjust if needed) then the script kills off bfgminer otherwise it does nothing. (Checking elapsed isn't really needed if you're careful to only run the monitor script after the miner gets up to speed but it was simple enough sanity check that it seemed worth the trouble adding it.)
So far this method is working quite well for me here but no promises.
I'd highly recommend you manually run ./monitor.sh while testing before adding it to your crontab to help troubleshoot. Once you're sure it's working as expected then add it to the crontab.
Some word of warning: Not much error or sanity checking is done to keep the script small & simple. For example you'll get an error if you run monitor.sh when bfgminer is not running, if RPC API isn't enabled, if the bfgminer was just started & RPC isn't ready, etc. So far the errors I've seen are benign & can be ignored. The script does nothing unless the stated criteria is met so it should be harmless if it hits a snag. The only issue I've seen myself is if I set the MINSPEED too high or if one of the miners is locked enough that it needs power cycled then the script will restart the bfgminer over & over at whatever interval you set in crontab. So keep that in mind & adjust accordingly. Also note that 20s speed is floating point but to keep it simple in bash script I "convert" to integer chopping off everything after the dot. I realize this isn't ideal but again was trying to keep it simple & it should be accurate enough for the purpose of monitoring. Obviously if your speed is less than 1 the script isn't going to work right so you'd need to adjust the script. Also note that this assumes . vs , so I'm not sure if bfgminer is localized so you'd need to modify the script for that if needed. You'd try SPEED=${SPEEDFLOAT%,*} instead perhaps.