setup-service.sh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/bin/bash
  2. # Setup script for absRecommend systemd service
  3. # This script installs and configures the service to run on boot with auto-restart
  4. set -e # Exit on error
  5. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  6. SERVICE_FILE="$SCRIPT_DIR/absrecommend.service"
  7. SERVICE_NAME="absrecommend"
  8. echo "========================================="
  9. echo "absRecommend Service Setup"
  10. echo "========================================="
  11. echo ""
  12. # Check if running as root
  13. if [ "$EUID" -eq 0 ]; then
  14. echo "ERROR: Please run this script as a regular user (not root/sudo)"
  15. echo "The script will prompt for sudo password when needed"
  16. exit 1
  17. fi
  18. # Check if service file exists
  19. if [ ! -f "$SERVICE_FILE" ]; then
  20. echo "ERROR: Service file not found at $SERVICE_FILE"
  21. exit 1
  22. fi
  23. # Stop any existing background processes
  24. echo "Stopping any running instances..."
  25. pkill -f "python.*main.py" || true
  26. sleep 2
  27. # Copy service file
  28. echo "Installing service file..."
  29. sudo cp "$SERVICE_FILE" /etc/systemd/system/
  30. # Reload systemd
  31. echo "Reloading systemd..."
  32. sudo systemctl daemon-reload
  33. # Enable service
  34. echo "Enabling service to start on boot..."
  35. sudo systemctl enable $SERVICE_NAME
  36. # Start service
  37. echo "Starting service..."
  38. sudo systemctl start $SERVICE_NAME
  39. # Wait a moment for service to start
  40. sleep 2
  41. # Check status
  42. echo ""
  43. echo "========================================="
  44. echo "Service Status:"
  45. echo "========================================="
  46. sudo systemctl status $SERVICE_NAME --no-pager || true
  47. echo ""
  48. echo "========================================="
  49. echo "Setup Complete!"
  50. echo "========================================="
  51. echo ""
  52. echo "The service is now running and will:"
  53. echo " - Start automatically on system boot"
  54. echo " - Restart automatically if it crashes"
  55. echo " - Wait 5 seconds between restart attempts"
  56. echo ""
  57. echo "Useful commands:"
  58. echo " View logs: sudo journalctl -u $SERVICE_NAME -f"
  59. echo " Restart: sudo systemctl restart $SERVICE_NAME"
  60. echo " Stop: sudo systemctl stop $SERVICE_NAME"
  61. echo " Start: sudo systemctl start $SERVICE_NAME"
  62. echo " Status: sudo systemctl status $SERVICE_NAME"
  63. echo " Disable startup: sudo systemctl disable $SERVICE_NAME"
  64. echo ""
  65. echo "Application running at: http://0.0.0.0:8000"
  66. echo ""