-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathservice.sh
More file actions
executable file
·270 lines (222 loc) · 6.18 KB
/
Copy pathservice.sh
File metadata and controls
executable file
·270 lines (222 loc) · 6.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/bin/bash
# Telescope Detection System Service Management Script
SERVICE_NAME="telescope_detection"
SERVICE_FILE="telescope_detection.service"
SYSTEMD_DIR="/etc/systemd/system"
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
print_header() {
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}Telescope Detection Service Manager${NC}"
echo -e "${BLUE}========================================${NC}"
}
print_success() {
echo -e "${GREEN}✅ $1${NC}"
}
print_error() {
echo -e "${RED}❌ $1${NC}"
}
print_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
print_info() {
echo -e "${BLUE}ℹ️ $1${NC}"
}
check_root() {
if [ "$EUID" -ne 0 ]; then
print_error "This command requires sudo/root privileges"
exit 1
fi
}
install_service() {
print_header
echo "Installing service..."
check_root
# Get current user (the one who ran sudo)
SERVICE_USER="${SUDO_USER:-$USER}"
# Check if template exists
if [ ! -f "$PROJECT_DIR/$SERVICE_FILE.template" ]; then
print_error "Service template not found: $PROJECT_DIR/$SERVICE_FILE.template"
exit 1
fi
# Escape special characters for sed
ESCAPED_USER=$(printf '%s\n' "$SERVICE_USER" | sed 's/[\/&]/\\&/g')
ESCAPED_PROJECT_DIR=$(printf '%s\n' "$PROJECT_DIR" | sed 's/[\/&]/\\&/g')
# Generate service file from template
print_info "Generating service file for user: $SERVICE_USER"
sed -e "s|{{USER}}|$ESCAPED_USER|g" \
-e "s|{{PROJECT_DIR}}|$ESCAPED_PROJECT_DIR|g" \
"$PROJECT_DIR/$SERVICE_FILE.template" > "$PROJECT_DIR/$SERVICE_FILE"
print_success "Service file generated"
# Copy service file to systemd directory
cp "$PROJECT_DIR/$SERVICE_FILE" "$SYSTEMD_DIR/$SERVICE_NAME.service"
print_success "Service file copied to $SYSTEMD_DIR"
# Reload systemd
systemctl daemon-reload
print_success "Systemd daemon reloaded"
# Enable service to start on boot
systemctl enable "$SERVICE_NAME.service"
print_success "Service enabled (will start on boot)"
echo ""
print_success "Service installed successfully!"
echo ""
print_info "Next steps:"
echo " • Start service: sudo $0 start"
echo " • Check status: sudo $0 status"
echo " • View logs: sudo $0 logs"
}
uninstall_service() {
print_header
echo "Uninstalling service..."
check_root
# Stop service if running
systemctl stop "$SERVICE_NAME.service" 2>/dev/null
# Disable service
systemctl disable "$SERVICE_NAME.service" 2>/dev/null
# Remove service file
if [ -f "$SYSTEMD_DIR/$SERVICE_NAME.service" ]; then
rm "$SYSTEMD_DIR/$SERVICE_NAME.service"
print_success "Service file removed"
fi
# Reload systemd
systemctl daemon-reload
print_success "Systemd daemon reloaded"
print_success "Service uninstalled successfully!"
}
start_service() {
print_header
echo "Starting service..."
check_root
systemctl start "$SERVICE_NAME.service"
if [ $? -eq 0 ]; then
print_success "Service started successfully"
echo ""
sleep 2
show_status
else
print_error "Failed to start service"
exit 1
fi
}
stop_service() {
print_header
echo "Stopping service..."
check_root
systemctl stop "$SERVICE_NAME.service"
if [ $? -eq 0 ]; then
print_success "Service stopped successfully"
else
print_error "Failed to stop service"
exit 1
fi
}
restart_service() {
print_header
echo "Restarting service..."
check_root
systemctl restart "$SERVICE_NAME.service"
if [ $? -eq 0 ]; then
print_success "Service restarted successfully"
echo ""
sleep 2
show_status
else
print_error "Failed to restart service"
exit 1
fi
}
show_status() {
print_header
systemctl status "$SERVICE_NAME.service" --no-pager
}
show_logs() {
print_header
echo "Showing logs (press Ctrl+C to exit)..."
echo ""
if [ "$1" == "follow" ] || [ "$1" == "-f" ]; then
journalctl -u "$SERVICE_NAME.service" -f
else
journalctl -u "$SERVICE_NAME.service" -n 100 --no-pager
fi
}
enable_service() {
print_header
echo "Enabling service to start on boot..."
check_root
systemctl enable "$SERVICE_NAME.service"
print_success "Service enabled"
}
disable_service() {
print_header
echo "Disabling service from starting on boot..."
check_root
systemctl disable "$SERVICE_NAME.service"
print_success "Service disabled"
}
show_help() {
print_header
echo ""
echo "Usage: $0 [command]"
echo ""
echo "Commands:"
echo " install Install service (requires sudo)"
echo " uninstall Remove service (requires sudo)"
echo " start Start the service (requires sudo)"
echo " stop Stop the service (requires sudo)"
echo " restart Restart the service (requires sudo)"
echo " status Show service status"
echo " logs Show recent logs"
echo " logs -f Follow logs in real-time"
echo " enable Enable service to start on boot (requires sudo)"
echo " disable Disable service from starting on boot (requires sudo)"
echo " help Show this help message"
echo ""
echo "Examples:"
echo " sudo $0 install # First time setup"
echo " sudo $0 start # Start the service"
echo " $0 status # Check if running"
echo " $0 logs -f # Watch logs live"
echo ""
}
# Main script logic
case "$1" in
install)
install_service
;;
uninstall)
uninstall_service
;;
start)
start_service
;;
stop)
stop_service
;;
restart)
restart_service
;;
status)
show_status
;;
logs)
show_logs "$2"
;;
enable)
enable_service
;;
disable)
disable_service
;;
help|--help|-h)
show_help
;;
*)
show_help
exit 1
;;
esac