-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgo-trader.sh
executable file
·142 lines (131 loc) · 3.39 KB
/
algo-trader.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/bash
# Directories and paths
LOG_DIR="./trader_logs"
PID_DIR="./pids"
SCRIPT_DIR="traders"
PYTHON_BIN="python3"
CRON_SCHEDULE="0 0 * * *" # Daily at midnight for get_status.py
SCRIPT_PATH="./scripts/get_status.py"
STATUS_LOG_FILE="./stats/status.log"
CRON_JOB="$CRON_SCHEDULE $PYTHON_BIN $SCRIPT_PATH >> $STATUS_LOG_FILE 2>&1"
# Ensure directories exist
mkdir -p "$LOG_DIR" "$PID_DIR"
# Function to check if an array contains a value
contains_element () {
local e match="$1"
shift
for e; do [[ "$e" == "$match" ]] && return 0; done
return 1
}
# Start a specific script
start_script() {
script_name=$(basename "$1")
out_file="$LOG_DIR/${script_name%.*}.out"
touch "$out_file"
echo "Starting $script_name..."
module_path="${1%.py}"
module_name="${module_path//\//.}"
nohup $PYTHON_BIN -u -m $module_name >> $out_file 2>&1 &
echo $! > "$PID_DIR/${script_name%.*}.pid"
printf "%s started with PID %s.\n\n" "$script_name" "$(cat "$PID_DIR/${script_name%.*}.pid")"
}
# Stop a specific script
stop_script() {
script_name=$(basename "$1")
pid_file="$PID_DIR/${script_name%.*}.pid"
if [ -f "$pid_file" ]; then
PID=$(cat "$pid_file")
echo "Stopping script with PID $PID..."
kill "$PID"
rm "$pid_file"
printf "Script %s stopped.\n\n" "$script_name"
else
echo "PID file for $script_name does not exist. Script is not running or was already stopped."
fi
}
# Start all scripts, excluding specified exceptions
start_scripts() {
echo "Starting scripts in $SCRIPT_DIR"
for script in $SCRIPT_DIR/*.py; do
script_name=$(basename "$script" .py)
if contains_element "$(basename "$script_name")" "$@"; then
echo "Skipping $(basename "$script")"
continue
fi
start_script "$script"
done
}
# Stop all scripts, excluding specified exceptions
stop_scripts() {
echo "Stopping scripts, excluding specified exceptions..."
for pid_file in $PID_DIR/*.pid; do
script_name="${pid_file#$PID_DIR/}"
script_name="${script_name%.pid}.py"
if contains_element "$script_name" "$@"; then
echo "Skipping stop for $script_name"
continue
fi
stop_script "$SCRIPT_DIR/$script_name"
done
}
# Restart all scripts, excluding specified exceptions
restart_scripts() {
stop_scripts "$@"
start_scripts "$@"
}
# Cron job management for get_status.py
manage_cron_job() {
action="$1"
case "$action" in
start)
echo "Setting up daily cron job for get_status.py..."
(crontab -l 2>/dev/null; echo "$CRON_JOB") | crontab -
echo "Cron job added."
;;
stop)
echo "Removing cron job for get_status.py..."
crontab -l | grep -v "$SCRIPT_PATH" | crontab -
echo "Cron job removed."
;;
*)
echo "Invalid action for manage_cron_job: $action"
exit 1
;;
esac
}
# Main command handling
case "$1" in
start)
shift
if [ "$1" == "get_status" ]; then
manage_cron_job start
else
start_scripts "$@"
fi
;;
stop)
shift
if [ "$1" == "get_status" ]; then
manage_cron_job stop
else
stop_scripts "$@"
fi
;;
restart)
shift
if [ "$1" == "get_status" ]; then
manage_cron_job stop
manage_cron_job start
else
restart_scripts "$@"
fi
;;
-s) # Start a specific script
shift
start_script "$SCRIPT_DIR/$1.py"
;;
*)
echo "Usage: $0 {start|stop|restart|-s script_name} [exceptions...]"
exit 1
;;
esac