Litecoin minerd startup and idle detection script in bash for linux

From Urandom
Jump to: navigation, search

I use this script to run litecoin cpuminer. It changes the amount of miner threads depending on weather your pc is idle or not. This enabled me to use the computer normally when needed, but it will start mining when idle.


#!/bin/bash
cd /opt/miner/litecoin/

if [ "$3" == "" ]
then
echo "Usage start.sh THREADS IDLETHREADS IDLETIME"
echo "THREADS - number of threads to run when not idle"
echo "IDLETHREADS - number of threads to run when idle"
echo "IDLETIME - time in minutes when to concider being idle"
exit 1
fi

if [ "$4" != "nohupme" ] #just to fork it to background
then
nohup $0 $1 $2 $3 nohupme 2>&1 > start.log & 

else #actual program

#amount of threads when                                                                                                                                                                                                                                                        
idle=$2                                                                                                                                                                                                                                                                        
notidle=$1                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                               
#when to concider machine to be idle in s                                                                                                                                                                                                                                      
idletime=$3                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                               
idletime=`expr  60 \* 1000 \* $idletime` #converting minutes to milliseconds                                                                                                                                                                                                   
threads=$notidle                                                                                                                                                                                                                                                               
last=$notidle                                                                                                                                                                                                                                                                  
pid=0                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                               
while [ $pid -eq 0 -o "`pidof minerd`" == "$pid" ]                                                                                                                                                                                                                             
do                                                                                                                                                                                                                                                                             
 if [ `xprintidle` -gt $idletime ] #check how long x has been idle                                                                                                                                                                                                             
 then                                                                                                                                                                                                                                                                          
    threads=$idle                                                                                                                                                                                                                                                              
 else                                                                                                                                                                                                                                                                          
    threads=$notidle                                                                                                                                                                                                                                                           
 fi                                                                                                                                                                                                                                                                            
 if [ $threads -ne $last -o $pid -eq 0 ] #check if threads have changes or first run                                                                                                                                                                                           
 then
   if [ $pid -ne 0 ] #kill last miner if it ran
   then
     echo "Threads changed to $threads!"
     killall minerd
     sleep 1
   fi
   if [ $threads -gt 0 ]
   then
     /opt/miner/litecoin/minerd --background --syslog --algo scrypt --scantime 99 --url http://HOST:PORT --userpass USERNAME:PASSWORD --threads $threads
     pid="`pidof minerd`"
   else
     pid=0
   fi
   last=$threads
 fi
 sleep 10 #check every 10 seconds
done 

fi


Personal tools