Skip to content

Instantly share code, notes, and snippets.

@erincerys
Last active October 11, 2017 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erincerys/b1dd66a0739e2a7d2d37039225b3e7ef to your computer and use it in GitHub Desktop.
Save erincerys/b1dd66a0739e2a7d2d37039225b3e7ef to your computer and use it in GitHub Desktop.
Schedule CPU utilization of your cryptocurrency miner around your personal computer use patterns
#!/bin/bash
#
# usage: $0 <cpu-threads>
#
# i use this script to schedule times of day when my computer is not in use can max out cpu on the miner
#
# example cron entries:
# 00 06 * * * /path/to/xmr-mining-scheduler.sh 3
# 00 23 * * * /path/to/xmr-mining-scheduler.sh 4
## config
MinerProcName='xmr-stak-cpu'
MinerBinPath="${HOME}/pkg/xmr-stak-cpu/bin/xmr-stak-cpu"
MinerPidPath="${HOME}/pkg/xmr-stak-cpu/run/${MinerProcName}.pid"
# whether or not to fork the miner to the background
MinerFork=0
MinerThreads=$1
# feel free to tweak this to your system
case $MinerThreads in
[3])
MinerConfPath="${HOME}/pkg/xmr-stak-cpu/conf/config.txt"
;;
[4])
MinerConfPath="${HOME}/pkg/xmr-stak-cpu/conf/config-allcores.txt"
;;
*)
echo "Invalid number of threads! Please choose either 3 or 4"
exit 1
;;
esac
## end config
## functions
function kill-pid () {
if [ $1 ] ; then
echo 'Sending sigterm'
kill -15 $1
sleep 2
[ "`pgrep $MinerProcName`" ] && { echo 'Sending sighup' ; pkill -9 $1 ; sleep 1 ; }
fi
}
## kill old miner process
# if the pid file exists
if [ -e $MinerPidPath ] ; then
MinerPid=`cat $MinerPidPath`
# try to kill the pid if its still running
if [[ "$MinerPid" && -d /proc/${MinerPid} ]] ; then
kill-pid $MinerPid
# otherwise, check for a different pid for the same miner
else
MinerPid=`pgrep $MinerProcName`
# kill if running
[ "$MinerPid" ] && { kill-pid $MinerPid ; } || { echo 'Miner not running' ; }
fi
# if the pid file doesnt exist
else
MinerPid=`pgrep $MinerProcName`
# kill if running
[ "$MinerPid" ] && { kill-pid $MinerPid ; } || { echo 'Miner not running' ; }
fi
## launch replacement process
echo 'Launcing miner'
[[ $MinerFork -eq 1 ]] { $MinerBinPath $MinerConfPath 2>&1 > /dev/null & ; } || { $MinerBinPath $MinerConfPath }
## if the miner is forked, store the pid
# store pid
sleep 1
MinerPid=`pgrep $MinerProcName`
if [ "$MinerPid" ] ; then
echo $MinerPid > $MinerPidPath
else
echo "Something went wrong. Miner may not have started"
rm $MinerPidPath
fi
#eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment