-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-service.sh
More file actions
executable file
·235 lines (204 loc) · 7.18 KB
/
install-service.sh
File metadata and controls
executable file
·235 lines (204 loc) · 7.18 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/bin/bash
# Plex Tuner Service Installation Script
# This script installs plex-tuner as a systemd service to run at boot
set -e # Exit on error
echo "🎵 Plex Tuner Service Installation"
echo "=================================="
echo ""
# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo "❌ This script must be run as root (use sudo)"
echo " sudo ./install-service.sh"
exit 1
fi
# Check if systemd is available
if ! command -v systemctl &> /dev/null; then
echo "❌ systemctl not found. This script requires systemd."
echo " This script is designed for systemd-based systems (Raspberry Pi OS, Ubuntu, etc.)"
exit 1
fi
# Check if systemd directory exists
SYSTEMD_DIR="/etc/systemd/system"
if [ ! -d "$SYSTEMD_DIR" ]; then
echo "❌ Systemd directory not found: $SYSTEMD_DIR"
echo " This system may not use systemd"
exit 1
fi
# Get the directory where this script is located
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SERVICE_FILE="$SCRIPT_DIR/plex-tuner.service"
# Check if service file exists
if [ ! -f "$SERVICE_FILE" ]; then
echo "❌ Service file not found: $SERVICE_FILE"
exit 1
fi
# Get the actual user (not root)
if [ -n "$SUDO_USER" ]; then
ACTUAL_USER="$SUDO_USER"
elif [ -n "$USER" ] && [ "$USER" != "root" ]; then
ACTUAL_USER="$USER"
else
# Try to get the actual user who invoked sudo
ACTUAL_USER=$(logname 2>/dev/null || who am i | awk '{print $1}' | head -1)
if [ -z "$ACTUAL_USER" ] || [ "$ACTUAL_USER" = "root" ]; then
# Last resort: try to find a non-root user
ACTUAL_USER=$(getent passwd | awk -F: '$3 >= 1000 && $1 != "nobody" {print $1; exit}')
if [ -z "$ACTUAL_USER" ]; then
echo "❌ Could not determine user. Please run as: sudo -u <username> ./install-service.sh"
exit 1
fi
fi
fi
# Get the actual home directory
ACTUAL_HOME=$(eval echo ~$ACTUAL_USER 2>/dev/null || echo "/home/$ACTUAL_USER")
# Verify user exists
if ! id "$ACTUAL_USER" &>/dev/null; then
echo "❌ User '$ACTUAL_USER' does not exist"
exit 1
fi
# Detect the actual project directory
PROJECT_DIR="$SCRIPT_DIR"
# Update service file with actual paths
echo "📝 Configuring service:"
echo " User: $ACTUAL_USER"
echo " Home: $ACTUAL_HOME"
echo " Project: $PROJECT_DIR"
echo ""
# Create a temporary service file with correct paths
TEMP_SERVICE=$(mktemp)
# Escape special characters in paths for sed
ESCAPED_PROJECT_DIR=$(echo "$PROJECT_DIR" | sed 's/[[\.*^$()+?{|]/\\&/g')
ESCAPED_VENV_PATH=$(echo "$PROJECT_DIR/venv/bin/python3" | sed 's/[[\.*^$()+?{|]/\\&/g')
# Get Python executable path (use detected venv or system python3)
if [ -n "$VENV_DIR" ]; then
PYTHON_PATH="$VENV_DIR/bin/python3"
else
# Find system python3
PYTHON_PATH=$(command -v python3 || echo "/usr/bin/python3")
fi
# Escape Python path for sed
ESCAPED_PYTHON_PATH=$(echo "$PYTHON_PATH" | sed 's/[[\.*^$()+?{|]/\\&/g')
# Replace paths and user in service file
sed -e "s|/home/pi/code/plex-tuner|$ESCAPED_PROJECT_DIR|g" \
-e "s|User=pi|User=$ACTUAL_USER|g" \
-e "s|Group=pi|Group=$ACTUAL_USER|g" \
-e "s|/home/pi/code/plex-tuner/venv/bin/python3|$ESCAPED_PYTHON_PATH|g" \
"$SERVICE_FILE" > "$TEMP_SERVICE"
# Detect virtual environment (could be venv, env, .venv, etc.)
VENV_DIR=""
for venv_name in "venv" "env" ".venv"; do
if [ -d "$PROJECT_DIR/$venv_name" ] && [ -f "$PROJECT_DIR/$venv_name/bin/python3" ]; then
VENV_DIR="$PROJECT_DIR/$venv_name"
break
fi
done
if [ -z "$VENV_DIR" ]; then
echo "⚠️ Warning: Virtual environment not found"
echo " Looked for: venv/, env/, .venv/"
echo " Please run ./setup.sh first"
read -p "Continue anyway? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
rm -f "$TEMP_SERVICE"
exit 1
fi
# Use system python3 as fallback
PYTHON_EXEC="python3"
echo "⚠️ Will use system python3 (not recommended)"
else
PYTHON_EXEC="$VENV_DIR/bin/python3"
echo "✓ Found virtual environment: $VENV_DIR"
fi
# Verify Python executable exists
if [ ! -f "$PYTHON_EXEC" ] && [ "$PYTHON_EXEC" != "python3" ]; then
echo "❌ Python executable not found: $PYTHON_EXEC"
rm -f "$TEMP_SERVICE"
exit 1
fi
# Check if python3 is available (either in venv or system)
if ! command -v "$PYTHON_EXEC" &> /dev/null && ! "$PYTHON_EXEC" --version &> /dev/null; then
echo "❌ Python3 not found or not executable"
rm -f "$TEMP_SERVICE"
exit 1
fi
# Check if plex_tuner package exists
if [ ! -d "$PROJECT_DIR/plex_tuner" ]; then
echo "❌ plex_tuner package not found at $PROJECT_DIR/plex_tuner"
rm -f "$TEMP_SERVICE"
exit 1
fi
# Check if service already exists
SERVICE_NAME="plex-tuner.service"
SERVICE_PATH="$SYSTEMD_DIR/$SERVICE_NAME"
if [ -f "$SERVICE_PATH" ]; then
echo "⚠️ Service file already exists: $SERVICE_PATH"
read -p "Overwrite existing service? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Installation cancelled."
rm -f "$TEMP_SERVICE"
exit 0
fi
# Stop and disable existing service first
if systemctl is-active --quiet "$SERVICE_NAME" 2>/dev/null; then
echo "🛑 Stopping existing service..."
systemctl stop "$SERVICE_NAME" || true
fi
if systemctl is-enabled --quiet "$SERVICE_NAME" 2>/dev/null; then
systemctl disable "$SERVICE_NAME" || true
fi
fi
# Copy service file to systemd directory
echo "📦 Installing service file..."
cp "$TEMP_SERVICE" "$SERVICE_PATH"
rm -f "$TEMP_SERVICE"
# Set proper permissions
chmod 644 "$SERVICE_PATH"
# Verify service file syntax
echo "🔍 Validating service file..."
if ! systemd-analyze verify "$SERVICE_NAME" 2>/dev/null; then
echo "⚠️ Warning: Service file validation had issues (may still work)"
fi
# Reload systemd
echo "🔄 Reloading systemd daemon..."
if ! systemctl daemon-reload; then
echo "❌ Failed to reload systemd daemon"
exit 1
fi
# Enable service to start at boot
echo "✅ Enabling service to start at boot..."
if ! systemctl enable "$SERVICE_NAME"; then
echo "❌ Failed to enable service"
exit 1
fi
echo ""
echo "✅ Service installed successfully!"
echo ""
echo "Service commands:"
echo " Start service: sudo systemctl start plex-tuner"
echo " Stop service: sudo systemctl stop plex-tuner"
echo " Restart service: sudo systemctl restart plex-tuner"
echo " Check status: sudo systemctl status plex-tuner"
echo " View logs: sudo journalctl -u plex-tuner -f"
echo " Disable service: sudo systemctl disable plex-tuner"
echo ""
# Check if running in interactive mode (not piped/redirected)
if [ -t 0 ]; then
read -p "Start the service now? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
if systemctl start "$SERVICE_NAME"; then
echo "✅ Service started!"
echo ""
echo "Checking status..."
sleep 2
systemctl status "$SERVICE_NAME" --no-pager -l || true
else
echo "❌ Failed to start service"
echo "Check logs with: sudo journalctl -u $SERVICE_NAME -n 50"
exit 1
fi
fi
else
echo "💡 Run 'sudo systemctl start $SERVICE_NAME' to start the service"
fi