#!/bin/bash
# v20190315222508CET
# startlendingbot.sh is a somewhat simple script
# to start and if necessary re-start a lendingbot
# based on https://github.com/BitBotFactory/MikaLendingBot
# with some modifications (debug logging and such..)
#
# Defaults
LOOP=NO
VERBOSE=NO
DEBUG=NO
TESTRUN=NO
PYTHONEXE="$(which python2)"
BOTPATH=$( cd "$( dirname "$0" )" && pwd )
BOTSCRIPT="lendingbot.py"
BOTCOMMAND=""
CONFIGFILE=""
# my default cmdline
# --path=/home/hackbyte/LendingBot --config=/home/hackbyte/lendingbot_poloniex.cfg --loop --debug --verbose --test
################################################################################
########################################
####################
####################
########################################
################################################################################
function mytimestamp {
date '+%Y-%m-%dT%H:%M:%S.%N'
}
################################################################################
function verboseout {
if [ "${VERBOSE}" == "YES" ] ; then
printf "$@"
fi
}
################################################################################
function startbot {
verboseout "$(mytimestamp) Starting: %s\n" "time ${BOTCOMMAND}"
#if [ "${DEBUG}" == "YES" ] ; then verboseout "+ time $BOTCOMMAND\n" ; fi
time ${BOTCOMMAND}
verboseout "$(mytimestamp) Script ended.\n"
}
################################################################################
function loopbot {
while true ; do
startbot
if [ ! "${TESTRUN}" == "YES" ] ; then
if [ "${VERBOSE}" == "YES" ] ; then
verboseout "\nSleeping.."
sleep 1s
verboseout "."
sleep 1s
verboseout "."
sleep 1s
verboseout ".done\n"
else
sleep 3
fi
fi
# for a testrun, do not really loop...... ;)
if [ "${TESTRUN}" == "YES" ] ; then break ; fi
done
}
################################################################################
function printhelp {
formatstring="%-3s|%-15s %s\n"
#printf "${formatstring}" "-|--" ""
printf "${formatstring}" "-p=" "--path=" "Set bot path."
printf "${formatstring}" "-D" "--debug" "Enable debug outputs."
printf "${formatstring}" "v-" "--verbose" "Enable verbose output."
printf "${formatstring}" "-t" "--test" "Don't send actual orders and cancel loop."
printf "${formatstring}" "-c=" "--config=" "Use specified config for the bot."
printf "${formatstring}" "-s" "--script=" "Use script name instead of default lendingbot.py."
printf "${formatstring}" "-l" "--loop" "Enable loop mode, re-starting script if it fails/ends."
printf "${formatstring}" "-h" "--help" "Print this help message and exit."
}
################################################################################
########################################
####################
####################
########################################
################################################################################
# read cmdline arguments....
for i in "$@" ; do
case $i in
-p=*|--path=*)
BOTPATH="${i#*=}"
shift # past argument=value
;;
-D|--debug)
DEBUG=YES
shift # past argument=value
;;
-v|--verbose)
VERBOSE=YES
shift # past argument=value
;;
-t|--test)
TESTRUN=YES
shift # past argument=value
;;
-c=*|--configfile=*|--config=*)
CONFIGFILE="${i#*=}"
shift # past argument=value
;;
-s=*|--scriptname=*|--script=*)
BOTSCRIPT="${i#*=}"
shift # past argument=value
;;
-l|--loop|--loop=YES)
LOOP=YES
shift # past argument with no value
;;
-h|--help)
printhelp
exit 0
;;
*)
# unknown option
echo "Unknown option, '$i'!"
printhelp
exit 1
;;
esac
done
BOTCOMMAND="${PYTHONEXE} ${BOTSCRIPT}"
if [ ! -z "${CONFIGFILE}" ]; then
BOTCOMMAND="${BOTCOMMAND} --config ${CONFIGFILE}"
fi
if [ "${DEBUG}" == "YES" ] ; then
BOTCOMMAND="${BOTCOMMAND} --debug"
fi
if [ "${TESTRUN}" == "YES" ] ; then
BOTCOMMAND="${BOTCOMMAND} --dryrun"
fi
if [ "${DEBUG}" == "YES" ] ; then
foostring='%-15s = %s\n'
verboseout "$(mytimestamp) ${foostring}" LOOP "${LOOP}"
verboseout "$(mytimestamp) ${foostring}" VERBOSE "${VERBOSE}"
verboseout "$(mytimestamp) ${foostring}" DEBUG "${DEBUG}"
verboseout "$(mytimestamp) ${foostring}" TESTRUN "${TESTRUN}"
verboseout "$(mytimestamp) ${foostring}" CONFIGFILE "${CONFIGFILE}"
verboseout "$(mytimestamp) ${foostring}" PYTHONEXE "${PYTHONEXE}"
verboseout "$(mytimestamp) ${foostring}" BOTPATH "${BOTPATH}"
verboseout "$(mytimestamp) ${foostring}" BOTSCRIPT "${BOTSCRIPT}"
verboseout "$(mytimestamp) ${foostring}" BOTCOMMAND "${BOTCOMMAND}"
fi
pushd "${BOTPATH}" &>/dev/null
if [ "${LOOP}" == "YES" ] ; then
loopbot
else
startbot
fi
popd &>/dev/null
################################################################################
########################################
####################
exit 0
Comments