From 4069b5cd11d275fa7b8cfd36decf9787e231632e Mon Sep 17 00:00:00 2001 From: Oleg Sheynin Date: Sun, 6 Aug 2023 16:27:41 -0400 Subject: [PATCH] added trader_app start script --- scripts/trader_app_start.sh | 140 ++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 scripts/trader_app_start.sh diff --git a/scripts/trader_app_start.sh b/scripts/trader_app_start.sh new file mode 100644 index 0000000..bbf6e7b --- /dev/null +++ b/scripts/trader_app_start.sh @@ -0,0 +1,140 @@ +#!/bin/bash +Script="${0} ${*}" + +function usage { + echo "Usage: ${0} [--restart] [--stop] [--once] [--sleep_sec=]" + exit 1 +} + +for arg in "${@}" +do + case ${arg} in + --restart) + Restart=Y + shift + ;; + --stop) + Stop="Y" + shift + ;; + --once) + Once="Y" + shift + ;; + --inst_set=*) + InstrSet="${arg#*=}" + shift + ;; + --sleep_sec=*) + SleepSec="${arg#*=}" + shift + ;; + -*|--*) + usage + ;; + *) + ;; + esac +done + + +if [ "${SleepSec}" == "" ] +then + SleepSec=3 +fi + + +# -------------------- S e t t i n g s +RootDir=/home/cvtt/prod +ConfigFile="${RootDir}/config/trader_app.cfg" + +AdminPort="7210" +Exchange=COINBASE_AT +Name=trader_app +# -------------------- S e t t i n g s + +LogFile="${RootDir}/logs/$(date '+%Y%m%d_%H%M%S').${Name}.log" + +source ${HOME}/.pyenv/python3.10-venv/bin/activate +export PYTHONPATH=${RootDir} + +Cmd="nohup" +Cmd="${Cmd} python3" +Cmd="${Cmd} ${RootDir}/cvttpy/apps/trader_app.py" +Cmd="${Cmd} --config=${ConfigFile}" +Cmd="${Cmd} --active_exchanges=${Exchange}" +Cmd="${Cmd} --admin_port=${AdminPort}" +Cmd="${Cmd} --log_level=INFO" +Cmd="${Cmd} --log_file=${LogFile}" +Cmd="${Cmd} &" + +function start_it { + echo ${Cmd} + eval ${Cmd} & + sleep 10 +} + + +function check_it { + + if !(timeout 3 curl -s localhost:${AdminPort}/ping > /dev/null) + then + echo "${Script} doesn't respond. restarting..." + pids=$(ps -ef | grep "admin_port=${AdminPort}" | grep -v grep | tr -s ' ' |cut -d' ' -f2) + echo pids=${pids} + if [ "${pids}" != "" ] + then + kill -9 ${pids} + fi + start_it + fi +} + +function kill_it { + pids=$(ps -ef |grep $(basename ${0}) |grep -v ${$} |grep -v grep |tr -s ' ' |cut -d' ' -f2) + + if [ "${pids}" != "" ] + then + echo "Killing ${pids} ..." + kill -9 ${pids} + fi + while (timeout 3 curl -s localhost:${AdminPort}/ping > /dev/null) + do + echo "Shutting down localhost:${AdminPort}/shutdown ..." + timeout 5 curl -s localhost:${AdminPort}/shutdown + sleep 3 + + pids=$(ps -ef | grep "admin_port=${AdminPort}" | grep -v grep | tr -s ' ' |cut -d' ' -f2) + echo pids=${pids} + if [ "${pids}" != "" ] + then + kill -9 ${pids} + fi + done +} + +if [ "${Stop}" == "Y" ] ; then + kill_it + exit 0 +fi + +if [ "${Once}" == "Y" ] ; then + start_it + exit 0 +fi + + +if [ "${Restart}" == "Y" ] +then + kill_it +fi + +start_it +while true +do + check_it + echo "${Script} is checked" | /usr/bin/ts '[%Y-%m-%d %H:%M:%S]' + sleep ${SleepSec} +done + +