#!/bin/bash # Define paths SCRIPT_DIR="$HOME/bin" SCRIPT_PATH="$SCRIPT_DIR/pistarlog2ws.py" SERVICE_PATH="/etc/systemd/system/pistarlog2ws.service" # Ensure required packages are installed echo "Installing dependencies..." sudo apt update sudo apt install -y python3 python3-pip python3-aiofiles python3-websockets python3-tz # Create the script directory if it does not exist mkdir -p "$SCRIPT_DIR" # Copy the provided pistarlog2ws.py script if [[ -f "pistarlog2ws.py" ]]; then echo "Copying pistarlog2ws.py to $SCRIPT_PATH..." cp pistarlog2ws.py "$SCRIPT_PATH" else echo "Error: pistarlog2ws.py not found in the current directory!" exit 1 fi # Create the systemd service file echo "Creating systemd service..." sudo tee "$SERVICE_PATH" > /dev/null << EOF [Unit] Description=Log to WebSocket Server After=network.target [Service] ExecStart=/usr/bin/python3 $SCRIPT_PATH Restart=always User=$USER WorkingDirectory=$SCRIPT_DIR StandardOutput=append:/var/log/pistarlog2ws.log StandardError=append:/var/log/pistarlog2ws.log RestartSec=5 [Install] WantedBy=multi-user.target EOF # Open netfilter port echo "Opening port (Netfilter)..." sudo tee /root/ipv4.fw > /dev/null << EOF iptables -I INPUT 1 -p tcp --dport 8765 -j ACCEPT EOF sudo pistar-firewall # Reload systemd to recognize new service echo "Enabling and starting the service..." sudo systemctl daemon-reload sudo systemctl enable pistarlog2ws.service sudo systemctl start pistarlog2ws.service echo "Installation complete. Use the following commands to manage the service:" echo " sudo systemctl start pistarlog2ws.service # Start the service" echo " sudo systemctl stop pistarlog2ws.service # Stop the service" echo " sudo systemctl restart pistarlog2ws.service # Restart the service" echo " sudo systemctl status pistarlog2ws.service # Check the status"