Advertisement
hackbyte

startlendingbot.sh

Mar 15th, 2019
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.54 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #   v20190315222508CET
  4. #       startlendingbot.sh is a somewhat simple script
  5. #       to start and if necessary re-start a lendingbot
  6. #       based on https://github.com/BitBotFactory/MikaLendingBot
  7. #       with some modifications (debug logging and such..)
  8. #
  9.  
  10.  
  11. # Defaults
  12. LOOP=NO
  13. VERBOSE=NO
  14. DEBUG=NO
  15. TESTRUN=NO
  16.  
  17. PYTHONEXE="$(which python2)"
  18.  
  19. BOTPATH=$( cd "$( dirname "$0" )" && pwd )
  20.  
  21. BOTSCRIPT="lendingbot.py"
  22. BOTCOMMAND=""
  23.  
  24. CONFIGFILE=""
  25.  
  26. # my default cmdline
  27. # --path=/home/hackbyte/LendingBot --config=/home/hackbyte/lendingbot_poloniex.cfg --loop --debug --verbose --test
  28.  
  29. ################################################################################
  30. ########################################
  31. ####################
  32.  
  33.  
  34. ####################
  35. ########################################
  36. ################################################################################
  37. function mytimestamp {
  38.     date '+%Y-%m-%dT%H:%M:%S.%N'
  39. }
  40. ################################################################################
  41. function verboseout {
  42.     if [ "${VERBOSE}" == "YES" ] ; then
  43.         printf "$@"
  44.     fi
  45. }
  46. ################################################################################
  47. function startbot {
  48.     verboseout "$(mytimestamp) Starting:         %s\n" "time ${BOTCOMMAND}"
  49.     #if [ "${DEBUG}"   == "YES" ] ; then verboseout "+ time $BOTCOMMAND\n" ; fi
  50.     time ${BOTCOMMAND}
  51.     verboseout "$(mytimestamp) Script ended.\n"
  52. }
  53. ################################################################################
  54. function loopbot {
  55.     while true ; do
  56.         startbot
  57.         if [ ! "${TESTRUN}" == "YES" ] ; then
  58.             if [ "${VERBOSE}" == "YES" ] ; then
  59.                 verboseout "\nSleeping.."
  60.                 sleep 1s
  61.                 verboseout "."
  62.                 sleep 1s
  63.                 verboseout "."
  64.                 sleep 1s
  65.                 verboseout ".done\n"
  66.             else
  67.                 sleep 3
  68.             fi
  69.         fi
  70.         # for a testrun, do not really loop...... ;)
  71.         if [ "${TESTRUN}" == "YES" ] ; then break ; fi
  72.     done
  73. }
  74. ################################################################################
  75. function printhelp {
  76.     formatstring="%-3s|%-15s %s\n"
  77.     #printf "${formatstring}" "-|--" ""
  78.     printf "${formatstring}" "-p="  "--path="   "Set bot path."
  79.     printf "${formatstring}" "-D"   "--debug"   "Enable debug outputs."
  80.     printf "${formatstring}" "v-"   "--verbose" "Enable verbose output."
  81.     printf "${formatstring}" "-t"   "--test"    "Don't send actual orders and cancel loop."
  82.     printf "${formatstring}" "-c="  "--config=" "Use specified config for the bot."
  83.     printf "${formatstring}" "-s"   "--script=" "Use script name instead of default lendingbot.py."
  84.     printf "${formatstring}" "-l"   "--loop"    "Enable loop mode, re-starting script if it fails/ends."
  85.     printf "${formatstring}" "-h"   "--help"    "Print this help message and exit."
  86. }
  87. ################################################################################
  88. ########################################
  89. ####################
  90.  
  91.  
  92. ####################
  93. ########################################
  94. ################################################################################
  95.  
  96. # read cmdline arguments....
  97.  
  98. for i in "$@" ; do
  99.     case $i in
  100.         -p=*|--path=*)
  101.             BOTPATH="${i#*=}"
  102.             shift # past argument=value
  103.         ;;
  104.         -D|--debug)
  105.             DEBUG=YES
  106.             shift # past argument=value
  107.         ;;
  108.         -v|--verbose)
  109.             VERBOSE=YES
  110.             shift # past argument=value
  111.         ;;
  112.         -t|--test)
  113.             TESTRUN=YES
  114.             shift # past argument=value
  115.         ;;
  116.         -c=*|--configfile=*|--config=*)
  117.             CONFIGFILE="${i#*=}"
  118.             shift # past argument=value
  119.         ;;
  120.         -s=*|--scriptname=*|--script=*)
  121.             BOTSCRIPT="${i#*=}"
  122.             shift # past argument=value
  123.         ;;
  124.         -l|--loop|--loop=YES)
  125.             LOOP=YES
  126.             shift # past argument with no value
  127.         ;;
  128.         -h|--help)
  129.             printhelp
  130.             exit 0
  131.         ;;
  132.         *)
  133.             # unknown option
  134.             echo "Unknown option, '$i'!"
  135.             printhelp
  136.             exit 1
  137.         ;;
  138.     esac
  139. done
  140.  
  141. BOTCOMMAND="${PYTHONEXE} ${BOTSCRIPT}"
  142.  
  143. if [ ! -z "${CONFIGFILE}" ]; then
  144.     BOTCOMMAND="${BOTCOMMAND} --config ${CONFIGFILE}"
  145. fi
  146.  
  147. if [ "${DEBUG}" == "YES" ] ; then
  148.     BOTCOMMAND="${BOTCOMMAND} --debug"
  149. fi
  150.  
  151. if [ "${TESTRUN}" == "YES" ] ; then
  152.     BOTCOMMAND="${BOTCOMMAND} --dryrun"
  153. fi
  154.  
  155.  
  156. if [ "${DEBUG}" == "YES" ] ; then
  157.     foostring='%-15s = %s\n'
  158.     verboseout "$(mytimestamp) ${foostring}" LOOP       "${LOOP}"
  159.     verboseout "$(mytimestamp) ${foostring}" VERBOSE    "${VERBOSE}"
  160.     verboseout "$(mytimestamp) ${foostring}" DEBUG      "${DEBUG}"
  161.     verboseout "$(mytimestamp) ${foostring}" TESTRUN    "${TESTRUN}"
  162.     verboseout "$(mytimestamp) ${foostring}" CONFIGFILE "${CONFIGFILE}"
  163.     verboseout "$(mytimestamp) ${foostring}" PYTHONEXE  "${PYTHONEXE}"
  164.     verboseout "$(mytimestamp) ${foostring}" BOTPATH    "${BOTPATH}"
  165.     verboseout "$(mytimestamp) ${foostring}" BOTSCRIPT  "${BOTSCRIPT}"
  166.     verboseout "$(mytimestamp) ${foostring}" BOTCOMMAND "${BOTCOMMAND}"
  167. fi
  168.  
  169. pushd "${BOTPATH}" &>/dev/null
  170.  
  171. if [ "${LOOP}" == "YES" ] ; then
  172.     loopbot
  173. else
  174.     startbot
  175. fi
  176.  
  177. popd &>/dev/null
  178.  
  179. ################################################################################
  180. ########################################
  181. ####################
  182.  
  183. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement