Username: Password:

Author Topic: Simple bash script to start/stop a single pair in a Screen [LINUX]  (Read 26653 times)

Offline gionni

  • Rookie
  • *
  • Posts: 23
    • View Profile
I made up a little something for those who like to manage the bot manually.
Do take a look at this post by user shacky here if you want to start all your pairs at once instead:
https://gunthy.org/index.php?topic=129.0

Note that I'm using exactly the same command here to launch the bot and I respected the same screen naming. I haven't tested but I believe this script is compatible with shacky's script and you can use gunctl script to stop a single pair also launched with shacky's script.

- configure the botfolder in the script
- make the script executable: chmod u+x gunctl.sh

USAGE:
Start:
Code: [Select]
./gunctl.sh start [strategy] [pair] [exchange or leave blank for poloniex]
Stop:
Code: [Select]
./gunctl.sh stop [pair]
List (running pairs, strategy and exchange):
Code: [Select]
./gunctl.sh list [pair]
View the running bot:
Code: [Select]
screen -r PAIR_NAME
Code: [Select]
#!/bin/bash
########### EDIT PLEASE ###############
# exact folder where gunbot and config files are
botfolder="/your/path/to/gunbot/"
########## NO MORE EDIT ###############


############## DONT EDIT ##############

red=`tput setaf 1`
green=`tput setaf 2`
blue=`tput setaf 4`
reset=`tput sgr0`
cmd=$1
strat=$2
pair=$3
exchange=$4


echo "*********** GUNCTL - v0.0.2 - $( date ) ***************"

if [[ -n "$cmd" ]]; then
  if [[ "$cmd" == "list" ]]; then
  lookup='ps aux'
  printf "%-10s %-10s %-10s\n" "Strategy" "Pair" "Exchange"
  printf "%-10s %-10s %-10s\n" "----------" "----------" "----------"
  $lookup | grep -v grep | grep -v gunctl | grep "node [stepgain|BB|1000trades]" |  awk '{printf "%-10s %-10s %-10s\n",$12,$13,$14}' | sort -nk2
  exit 0
  fi 
  if [[ "$cmd" == "stop" ]]; then
    pair=$2
    if [[ -n "$pair" ]]; then
      if ! screen -list | grep -q "$2"; then
        echo "${red}Pair not running or not started with gunctl... ${reset}"
        exit 0
      else
        echo "${green}Stopping Pair $2${reset}"
        screen -S $2 -X quit
        echo "${blue}Pair $2 stopped.${reset}"
        sleep 0.2
        exit 0
      fi
    else
      echo "Pair is missing"
      exit 0
    fi

  fi
else
  echo "Command missing"
  echo "Usage: gunctl [start [strat] [pair] [exchange],stop [pair],list]"
  exit 0
fi
if [[ -n "$strat" ]]; then
  echo "Strategy: $2"
else
  echo "Strategy is missing"
  exit 0
fi
if [[ -n "$pair" ]]; then
  echo "Pair: $3"
else
  echo "Pair is missing"
  exit 0
fi
if [[ -n "$exchange" ]]; then
  echo "Exchange: $4"
else
  exchange="poloniex"
  echo "Exchange: Poloniex"
fi
cd $botfolder
echo ""
if ! ls | grep -qw "$3"; then
  echo "Config missing for $3"
  exit 0
fi
echo "Checking if pair $3 is running..."
if ! ps aux | grep -v grep | grep -v gunctl | grep "$3"; then
  echo "${red}Pair not running...${reset}"
  echo "${green}Starting $3 pair. ${reset}"

  screen -dmS $3
  screen -S $3 -p 0 -X exec node $2 $3 $4
else
  echo "${green}Pair $3 is already running${reset}"
  sleep 0.2
fi

« Last Edit: May 06, 2017, 04:52:38 PM by gionni »

Offline Clannad

  • Rookie
  • *
  • Posts: 38
    • View Profile
Re: Simple bash script to start/stop a single pair in a Screen [LINUX]
« Reply #1 on: May 06, 2017, 10:17:27 PM »
Nice work, thanks gionni for sharing this !

I'm especially interested with this part (you'll understand why if you read my latest post)

Stop:
Code: [Select]
screen -S $2 -X quit

Do you know in deep how screen stops a process associated with the window you quit ?

Offline gionni

  • Rookie
  • *
  • Posts: 23
    • View Profile
Re: Simple bash script to start/stop a single pair in a Screen [LINUX]
« Reply #2 on: May 07, 2017, 12:49:30 AM »
I believe that it kills all the processes running from the window and then quit the screen session

Offline Clannad

  • Rookie
  • *
  • Posts: 38
    • View Profile
Re: Simple bash script to start/stop a single pair in a Screen [LINUX]
« Reply #3 on: May 09, 2017, 08:10:11 AM »
I believe that it kills all the processes running from the window and then quit the screen session
Thanks for your answer :)
Well not exactly the kind of "smart" exit I am looking for ... but I guess there is no other way...

Offline gionni

  • Rookie
  • *
  • Posts: 23
    • View Profile
Re: Simple bash script to start/stop a single pair in a Screen [LINUX]
« Reply #4 on: May 11, 2017, 09:41:03 AM »
I believe that it kills all the processes running from the window and then quit the screen session
Thanks for your answer :)
Well not exactly the kind of "smart" exit I am looking for ... but I guess there is no other way...

What were you looking for?

Offline Clannad

  • Rookie
  • *
  • Posts: 38
    • View Profile
Re: Simple bash script to start/stop a single pair in a Screen [LINUX]
« Reply #5 on: May 12, 2017, 08:58:19 PM »
I believe that it kills all the processes running from the window and then quit the screen session
Thanks for your answer :)
Well not exactly the kind of "smart" exit I am looking for ... but I guess there is no other way...

What were you looking for?

A way to tell the process to close himself, thus making shure we don't kill it while it is buying or selling, which could lead to corrupted/inaccurate save.json file

I even suggested an easy way to code this here: https://gunthy.org/index.php?topic=257.0