170 lines
No EOL
4.6 KiB
Bash
Executable file
170 lines
No EOL
4.6 KiB
Bash
Executable file
#!/bin/bash
|
|
# VAMR Algorithm Service Script
|
|
# Usage: ./algorithm-service.sh {start|stop|restart|status}
|
|
|
|
# Get the script directory
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
|
|
# Navigate to the root directory (2 levels up from the script directory)
|
|
ROOT_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)"
|
|
|
|
# Define paths
|
|
PYTHON_PATH="$ROOT_DIR/venv/bin/python"
|
|
ALGORITHM_PATH="$ROOT_DIR/VAMR/VAMR/algorithm.py"
|
|
LOG_DIR="$ROOT_DIR/VAMR/logs"
|
|
LOG_ARCHIVE_DIR="$LOG_DIR/archive"
|
|
DATA_DIR="$ROOT_DIR/VAMR/data"
|
|
PID_FILE="$DATA_DIR/algorithm.pid"
|
|
STDOUT_LOG="$LOG_DIR/stdout.log"
|
|
ERROR_LOG="$LOG_DIR/error.log"
|
|
INFO_LOG="$LOG_DIR/info.log"
|
|
|
|
# Ensure directories exist
|
|
mkdir -p "$LOG_DIR"
|
|
mkdir -p "$LOG_ARCHIVE_DIR"
|
|
mkdir -p "$DATA_DIR"
|
|
|
|
# Function to start the algorithm
|
|
start_algorithm() {
|
|
echo "Starting VAMR trading algorithm..."
|
|
if [ -f "$PID_FILE" ]; then
|
|
PID=$(cat "$PID_FILE")
|
|
if ps -p $PID > /dev/null; then
|
|
echo "Algorithm is already running with PID: $PID"
|
|
return 1
|
|
else
|
|
echo "Removing stale PID file"
|
|
rm "$PID_FILE"
|
|
fi
|
|
fi
|
|
|
|
# Rotate log files: Move existing logs to archive with timestamp
|
|
if [ -f "$INFO_LOG" ]; then
|
|
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
|
|
echo "Archiving existing info.log to archive folder with timestamp $TIMESTAMP"
|
|
mv "$INFO_LOG" "$LOG_ARCHIVE_DIR/info_$TIMESTAMP.log"
|
|
fi
|
|
|
|
# Create empty logs
|
|
touch "$INFO_LOG"
|
|
|
|
# Change to root directory and activate virtual environment
|
|
cd "$ROOT_DIR"
|
|
source venv/bin/activate
|
|
|
|
# Start algorithm in the background
|
|
cd VAMR/VAMR
|
|
nohup python algorithm.py > "$STDOUT_LOG" 2> "$ERROR_LOG" & disown
|
|
|
|
# Save PID for later use
|
|
echo $! > "$PID_FILE"
|
|
echo "Algorithm started with PID: $(cat "$PID_FILE")"
|
|
return 0
|
|
}
|
|
|
|
# Function to stop the algorithm
|
|
stop_algorithm() {
|
|
echo "Stopping VAMR trading algorithm..."
|
|
if [ -f "$PID_FILE" ]; then
|
|
PID=$(cat "$PID_FILE")
|
|
if ps -p $PID > /dev/null; then
|
|
echo "Stopping algorithm with PID: $PID"
|
|
kill $PID
|
|
sleep 2
|
|
|
|
# Check if process is still running
|
|
if ps -p $PID > /dev/null; then
|
|
echo "Algorithm still running, forcing termination..."
|
|
kill -9 $PID
|
|
sleep 1
|
|
fi
|
|
|
|
# Verify process is stopped
|
|
if ps -p $PID > /dev/null; then
|
|
echo "ERROR: Failed to stop algorithm!"
|
|
return 1
|
|
else
|
|
echo "Algorithm stopped successfully."
|
|
rm "$PID_FILE"
|
|
return 0
|
|
fi
|
|
else
|
|
echo "No algorithm running with PID: $PID (stale PID file)"
|
|
rm "$PID_FILE"
|
|
return 0
|
|
fi
|
|
else
|
|
echo "No PID file found. Algorithm may not be running."
|
|
return 0
|
|
fi
|
|
}
|
|
|
|
# Function to check algorithm status
|
|
status_algorithm() {
|
|
echo "===== VAMR Trading Algorithm Status ====="
|
|
|
|
# Check if algorithm is running
|
|
if [ -f "$PID_FILE" ]; then
|
|
PID=$(cat "$PID_FILE")
|
|
if ps -p $PID > /dev/null; then
|
|
echo "Status: RUNNING"
|
|
echo "PID: $PID"
|
|
|
|
# Get runtime information
|
|
PROC_START=$(ps -p $PID -o lstart=)
|
|
echo "Running since: $PROC_START"
|
|
|
|
# Get memory usage
|
|
MEM_USAGE=$(ps -p $PID -o %mem=)
|
|
echo "Memory usage: $MEM_USAGE%"
|
|
|
|
# Check state file
|
|
STATE_FILE="$DATA_DIR/state.json"
|
|
if [ -f "$STATE_FILE" ]; then
|
|
STATE_SIZE=$(du -h "$STATE_FILE" | cut -f1)
|
|
STATE_TIME=$(date -r "$STATE_FILE" "+%Y-%m-%d %H:%M:%S")
|
|
echo "State file: $STATE_SIZE, last updated: $STATE_TIME"
|
|
else
|
|
echo "State file: NOT FOUND"
|
|
fi
|
|
|
|
# Show recent log entries
|
|
echo ""
|
|
echo "Recent log entries:"
|
|
echo "--- Last 5 lines of algorithm log ---"
|
|
tail -n 5 "$INFO_LOG"
|
|
return 0
|
|
else
|
|
echo "Status: NOT RUNNING (stale PID file)"
|
|
rm "$PID_FILE"
|
|
return 1
|
|
fi
|
|
else
|
|
echo "Status: NOT RUNNING (no PID file)"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Main logic
|
|
case "$1" in
|
|
start)
|
|
start_algorithm
|
|
;;
|
|
stop)
|
|
stop_algorithm
|
|
;;
|
|
restart)
|
|
stop_algorithm
|
|
sleep 2
|
|
start_algorithm
|
|
;;
|
|
status)
|
|
status_algorithm
|
|
;;
|
|
*)
|
|
echo "Usage: $0 {start|stop|restart|status}"
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
exit $? |