restart_bot.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/bin/bash
  2. # Hyperliquid Trading Bot Restart Script
  3. # This script safely restarts the bot with latest code and fresh logs
  4. set -e # Exit on any error
  5. echo "🤖 Hyperliquid Trading Bot Restart Script"
  6. echo "=========================================="
  7. # Check if running as correct user (optional - remove if not needed)
  8. # if [[ $EUID -eq 0 ]]; then
  9. # echo "❌ This script should not be run as root for security reasons"
  10. # exit 1
  11. # fi
  12. # Function to print timestamped messages
  13. log() {
  14. echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
  15. }
  16. # Function to check if service exists
  17. check_service() {
  18. if ! systemctl list-unit-files | grep -q "trading-bot.service"; then
  19. log "⚠️ trading-bot.service not found in systemctl"
  20. log "ℹ️ Continuing without systemctl operations..."
  21. return 1
  22. fi
  23. return 0
  24. }
  25. # Step 1: Stop the trading bot service
  26. log "🛑 Stopping trading bot service..."
  27. if check_service; then
  28. if systemctl is-active --quiet trading-bot.service; then
  29. sudo systemctl stop trading-bot.service
  30. log "✅ Service stopped successfully"
  31. # Wait a moment for graceful shutdown
  32. sleep 2
  33. # Verify it's stopped
  34. if systemctl is-active --quiet trading-bot.service; then
  35. log "❌ Service is still running after stop command"
  36. exit 1
  37. else
  38. log "✅ Service confirmed stopped"
  39. fi
  40. else
  41. log "ℹ️ Service was already stopped"
  42. fi
  43. else
  44. log "⚠️ Skipping service stop (service not found)"
  45. fi
  46. # Step 2: Pull latest code from git
  47. log "📡 Pulling latest code from git..."
  48. if git status &>/dev/null; then
  49. # Check if we have uncommitted changes
  50. if ! git diff-index --quiet HEAD --; then
  51. log "⚠️ You have uncommitted changes:"
  52. git status --porcelain
  53. read -p "Do you want to continue and potentially lose changes? (y/N): " -n 1 -r
  54. echo
  55. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  56. log "❌ Restart cancelled by user"
  57. exit 1
  58. fi
  59. log "⚠️ Proceeding with uncommitted changes..."
  60. fi
  61. # Save current branch
  62. CURRENT_BRANCH=$(git branch --show-current)
  63. log "ℹ️ Current branch: $CURRENT_BRANCH"
  64. # Pull latest changes
  65. if git pull origin main; then
  66. log "✅ Git pull completed successfully"
  67. else
  68. log "❌ Git pull failed"
  69. exit 1
  70. fi
  71. else
  72. log "⚠️ Not a git repository, skipping git pull"
  73. fi
  74. # Step 3: Remove old log file
  75. log "🗑️ Clearing old log file..."
  76. if [ -f "logs/trading_bot.log" ]; then
  77. rm logs/trading_bot.log
  78. log "✅ Old log file removed"
  79. else
  80. log "ℹ️ Log file doesn't exist (already clean)"
  81. fi
  82. # Ensure logs directory exists
  83. if [ ! -d "logs" ]; then
  84. mkdir -p logs
  85. log "✅ Created logs directory"
  86. fi
  87. # Step 4: Restart the trading bot service
  88. log "🚀 Starting trading bot service..."
  89. if check_service; then
  90. # Use reload-or-restart as requested
  91. sudo systemctl reload-or-restart trading-bot.service
  92. log "✅ Service restart command sent"
  93. # Wait a moment for startup
  94. sleep 3
  95. # Check if service started successfully
  96. if systemctl is-active --quiet trading-bot.service; then
  97. log "✅ Service is running successfully"
  98. # Show service status
  99. log "📊 Service status:"
  100. systemctl status trading-bot.service --no-pager -l
  101. else
  102. log "❌ Service failed to start"
  103. log "📋 Service logs:"
  104. sudo journalctl -u trading-bot.service --no-pager -l --since "1 minute ago"
  105. exit 1
  106. fi
  107. else
  108. log "⚠️ Skipping service restart (service not found)"
  109. log "ℹ️ You may need to start the bot manually"
  110. fi
  111. # Step 5: Optional - tail logs for a few seconds
  112. log "📋 Showing recent logs..."
  113. if [ -f "logs/trading_bot.log" ]; then
  114. echo "----------------------------------------"
  115. echo "Recent log entries:"
  116. tail -n 20 logs/trading_bot.log || log "⚠️ Could not read log file yet"
  117. else
  118. log "ℹ️ Log file not created yet, showing systemd logs:"
  119. sudo journalctl -u trading-bot.service --no-pager -l --since "30 seconds ago" | tail -n 10
  120. fi
  121. echo "=========================================="
  122. log "🎉 Bot restart completed successfully!"
  123. log "💡 Use 'tail -f logs/trading_bot.log' to monitor logs"
  124. log "💡 Use 'systemctl status trading-bot.service' to check service status"