fix: auto-detect working directory, install venv, configure ufw firewall, and sync Option B vitals in setup_service.sh
This commit is contained in:
+41
-5
@@ -5,11 +5,45 @@ set -e
|
||||
|
||||
SERVICE_NAME="contec-forwarder"
|
||||
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
|
||||
WORKING_DIR="/home/prathiyuman/Prathiyuman/ContecMonitor/fiveparaminte-main"
|
||||
|
||||
# Auto-detect script directory or fallback to standard path
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
if [ -f "${SCRIPT_DIR}/main.py" ]; then
|
||||
WORKING_DIR="${SCRIPT_DIR}"
|
||||
elif [ -d "/home/prathiyuman/Prathiyuman/ContecMonitor/fiveparaminte-main" ]; then
|
||||
WORKING_DIR="/home/prathiyuman/Prathiyuman/ContecMonitor/fiveparaminte-main"
|
||||
elif [ -d "/home/prathiyuman/ContecMonitor" ]; then
|
||||
WORKING_DIR="/home/prathiyuman/ContecMonitor"
|
||||
else
|
||||
WORKING_DIR="${PWD}"
|
||||
fi
|
||||
|
||||
echo "=== Detected Working Directory: ${WORKING_DIR} ==="
|
||||
|
||||
# Ensure virtual environment exists
|
||||
if [ ! -f "${WORKING_DIR}/venv/bin/python" ]; then
|
||||
echo "=== Virtual environment not found. Creating venv... ==="
|
||||
python3 -m venv "${WORKING_DIR}/venv" || sudo apt-get update && sudo apt-get install -y python3-venv python3-pip && python3 -m venv "${WORKING_DIR}/venv"
|
||||
fi
|
||||
|
||||
PYTHON_EXEC="${WORKING_DIR}/venv/bin/python"
|
||||
SCRIPT_PATH="${WORKING_DIR}/main.py"
|
||||
|
||||
echo "=== Creating systemd service file ==="
|
||||
# Ensure dependencies are installed
|
||||
if [ -f "${WORKING_DIR}/requirements.txt" ]; then
|
||||
echo "=== Installing/verifying dependencies ==="
|
||||
"${WORKING_DIR}/venv/bin/pip" install --upgrade pip
|
||||
"${WORKING_DIR}/venv/bin/pip" install -r "${WORKING_DIR}/requirements.txt"
|
||||
fi
|
||||
|
||||
# Open firewall ports for Contec monitor (Port 511) and Web API (Port 8006)
|
||||
if command -v ufw >/dev/null 2>&1; then
|
||||
echo "=== Configuring firewall (UFW) for Port 511 and 8006 ==="
|
||||
sudo ufw allow 511/tcp comment "Contec Patient Monitor CMS TCP" || true
|
||||
sudo ufw allow 8006/tcp comment "Contec Forwarder HTTP API" || true
|
||||
fi
|
||||
|
||||
echo "=== Creating systemd service file: ${SERVICE_FILE} ==="
|
||||
sudo bash -c "cat > ${SERVICE_FILE}" <<EOF
|
||||
[Unit]
|
||||
Description=Contec Patient Monitor Vital Signs Forwarder
|
||||
@@ -20,6 +54,7 @@ Type=simple
|
||||
User=root
|
||||
WorkingDirectory=${WORKING_DIR}
|
||||
Environment=PORT=8006
|
||||
Environment=PYTHONPATH=${WORKING_DIR}
|
||||
ExecStart=${PYTHON_EXEC} ${SCRIPT_PATH}
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
@@ -36,7 +71,7 @@ sudo systemctl daemon-reload
|
||||
echo "=== Enabling ${SERVICE_NAME} to start on boot ==="
|
||||
sudo systemctl enable ${SERVICE_NAME}
|
||||
|
||||
echo "=== Starting ${SERVICE_NAME} service ==="
|
||||
echo "=== Restarting ${SERVICE_NAME} service ==="
|
||||
sudo systemctl restart ${SERVICE_NAME}
|
||||
|
||||
echo "=== Service Status ==="
|
||||
@@ -45,6 +80,7 @@ sudo systemctl status ${SERVICE_NAME} --no-pager
|
||||
echo ""
|
||||
echo "=========================================================="
|
||||
echo "SUCCESS: Contec Forwarder is now running in the background!"
|
||||
echo "To check live service logs, run:"
|
||||
echo " sudo journalctl -u contec-forwarder -f"
|
||||
echo "Port 511 is open and listening for the Contec physical monitor."
|
||||
echo "To check live logs, run:"
|
||||
echo " sudo journalctl -u ${SERVICE_NAME} -f"
|
||||
echo "=========================================================="
|
||||
|
||||
Reference in New Issue
Block a user