Files
BytbitBTC/mother.sh

160 lines
4.3 KiB
Bash
Executable File

#!/bin/bash
# Configuration
SESSION_NAME="bybitbtc"
NTP_SERVER="pool.ntp.org"
MAX_TIME_OFFSET=0.5 # Seconds
PYTHON_VERSION="3.10" # Adjust as needed
# Colors for output
G='\033[0;32m'
R='\033[0;31m'
Y='\033[1;33m'
NC='\033[0m'
log() { echo -e "${G}[MOTHER]${NC} $1"; }
warn() { echo -e "${Y}[WARN]${NC} $1"; }
err() { echo -e "${R}[ERROR]${NC} $1"; }
# 1. Dependency Check & Installation
setup() {
log "Starting dependency installation..."
# Check for tmux
if ! command -v tmux &> /dev/null; then
err "tmux not found. Please install it."
exit 1
fi
# Go Services
for dir in "input" "onramp"; do
if [ -d "$dir" ]; then
log "Setting up Go service: $dir"
(cd "$dir" && go mod tidy)
fi
done
# Python Services
for dir in "analysis" "signals" "monitor"; do
if [ -d "$dir" ]; then
log "Setting up Python service: $dir"
(cd "$dir" && pipenv install)
fi
done
log "Setup complete."
}
# 2. Tick Verification (NTP Sync Check)
verify_time() {
log "Verifying system clock sync with $NTP_SERVER..."
if ! command -v ntpdate &> /dev/null; then
warn "ntpdate not installed. Skipping precise tick verification."
return
fi
# Query NTP offset without setting the clock (-q)
OFFSET=$(ntpdate -q $NTP_SERVER | tail -1 | awk '{print $6}' | tr -d '-')
if [ -z "$OFFSET" ]; then
err "Could not reach NTP server."
return
fi
log "Current time offset: $OFFSET seconds"
# Compare offset using bc (bash doesn't do float comparison well)
IS_SYNCED=$(echo "$OFFSET < $MAX_TIME_OFFSET" | bc)
if [ "$IS_SYNCED" -eq 1 ]; then
log "Time sync is within healthy parameters."
else
err "Clock desync detected ($OFFSET s)! Trades might be rejected or delayed."
read -p "Continue anyway? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
fi
}
# 3. Start Services in Tmux
start() {
verify_time
# Kill existing session if it exists
tmux kill-session -t $SESSION_NAME 2>/dev/null
log "Launching services in tmux session: $SESSION_NAME"
# 1. Start INPUT
log "Stage 1: Starting Input (Websocket)..."
tmux new-session -d -s $SESSION_NAME -n "input"
tmux send-keys -t $SESSION_NAME:input "cd input && go run input.go" C-m
sleep 3 # Wait for WS connection and first file creation
# 2. Start ONRAMP
log "Stage 2: Starting Onramp (DB Writer)..."
tmux new-window -t $SESSION_NAME -n "onramp"
tmux send-keys -t $SESSION_NAME:onramp "cd onramp && go run onramp.go" C-m
sleep 5 # Wait for candles.db to be initialized and first rows inserted
# 3. Start ANALYST
log "Stage 3: Starting Analyst (Indicators)..."
tmux new-window -t $SESSION_NAME -n "analysis"
tmux send-keys -t $SESSION_NAME:analysis "cd analysis && pipenv run python analyst.py" C-m
sleep 8 # Analyst needs time to calculate indicators for the first time
# 4. Start SIGNALS
log "Stage 4: Starting Signals (Logic)..."
tmux new-window -t $SESSION_NAME -n "signals"
tmux send-keys -t $SESSION_NAME:signals "cd signals && pipenv run python signals.py" C-m
sleep 3 # Wait for the Unix Socket (/tmp/signals.sock) to be created
# 5. Start MONITOR
log "Stage 5: Starting Monitor..."
tmux new-window -t $SESSION_NAME -n "monitor"
tmux send-keys -t $SESSION_NAME:monitor "cd monitor && pipenv run python monitor.py" C-m
log "${G}All services started in sequence.${NC}"
log "Use 'tmux attach -t $SESSION_NAME' to view logs."
log "Use './mother.sh stop' to kill all services."
}
stop() {
log "Stopping tmux session $SESSION_NAME..."
tmux kill-session -t $SESSION_NAME 2>/dev/null
log "Stopped."
}
status() {
if tmux has-session -t $SESSION_NAME 2>/dev/null; then
log "Status: ${G}RUNNING${NC}"
tmux list-windows -t $SESSION_NAME
else
log "Status: ${R}STOPPED${NC}"
fi
}
# Main Command Switch
case "$1" in
setup)
setup
;;
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {setup|start|stop|status|restart}"
exit 1
esac