Repository navigation
Expand file tree
/
Copy pathstart_services.sh
More file actions
58 lines (49 loc) · 1.63 KB
/
Copy pathstart_services.sh
File metadata and controls
58 lines (49 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash
# start_services.sh
# Starts the Flask web app and the print manager in the background with PID & log management
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "$0")" && pwd)"
PID_DIR="${ROOT_DIR}/run"
mkdir -p "${PID_DIR}"
WEB_LOG="${ROOT_DIR}/web_app.log" # keep existing underscore style already present on device
PM_LOG="${ROOT_DIR}/print_manager.log"
activate_venv() {
if [ -f "${ROOT_DIR}/venv/bin/activate" ]; then
# shellcheck disable=SC1090
source "${ROOT_DIR}/venv/bin/activate"
fi
}
already_running() {
local name="$1" pattern="$2"
if pgrep -f "$pattern" >/dev/null 2>&1; then
echo "$name appears to already be running (pattern: $pattern). Skipping start." >&2
return 0
fi
return 1
}
activate_venv
echo "Starting services from ${ROOT_DIR}" | tee -a "$WEB_LOG" "$PM_LOG"
# Start web app if not running
if ! already_running "Web App" "web-app/app.py"; then
(
cd "${ROOT_DIR}/web-app"
nohup python3 -u app.py >>"$WEB_LOG" 2>&1 &
WEB_PID=$!
echo "$WEB_PID" > "${PID_DIR}/web_app.pid"
echo "Web app started PID=$WEB_PID (log: $WEB_LOG)"
)
echo "Waiting 5 seconds for web app to initialize..."
sleep 5
fi
# Start print manager if not running
if ! already_running "Print Manager" "src/controller/print_manager.py"; then
(
cd "${ROOT_DIR}/src/controller"
nohup python3 -u print_manager.py >>"$PM_LOG" 2>&1 &
PM_PID=$!
echo "$PM_PID" > "${PID_DIR}/print_manager.pid"
echo "Print manager started PID=$PM_PID (log: $PM_LOG)"
)
fi
echo "Both services ensured running. Access the web UI at: http://$(hostname -I | awk '{print $1}'):5000" | tee -a "$WEB_LOG"
echo "Use ./stop_services.sh to stop them."