Schedule CPU utilization of your cryptocurrency miner around your personal computer use patterns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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