-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-service.sh
More file actions
64 lines (55 loc) · 2.29 KB
/
setup-service.sh
File metadata and controls
64 lines (55 loc) · 2.29 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
#!/bin/bash
# Define the service unit file content with absolute paths
SERVICE_UNIT="[Unit]
Description=escuplast-service
After=network.target
[Service]
ExecStart=/usr/local/bin/python3.9 /home/escuplast/ModBusServer/server.py
WorkingDirectory=/home/escuplast/ModBusServer
Restart=always
User=escuplast
[Install]
WantedBy=multi-user.target"
# Define the service unit file path
SERVICE_UNIT_FILE="/etc/systemd/system/escuplast-service.service"
# Ensure the ModBusServer directory exists and set the correct ownership
if [ ! -d "/home/escuplast/ModBusServer" ]; then
sudo mkdir -p /home/escuplast/ModBusServer
fi
sudo chown -R escuplast:escuplast /home/escuplast/ModBusServer
# Check if the server script exists
if [ ! -f "/home/escuplast/ModBusServer/server.py" ]; then
echo "Server script not found in /home/escuplast/ModBusServer. Please ensure server.py is in place."
exit 1
fi
# Set permissions for the server script
sudo chmod +x /home/escuplast/ModBusServer/server.py
# Check if the service unit file already exists
if [ -f "$SERVICE_UNIT_FILE" ]; then
echo "Service unit file already exists. Updating the file..."
# Stop and disable the service before updating the file
sudo systemctl stop escuplast-service
sudo systemctl disable escuplast-service
# Remove the existing service file
sudo rm "$SERVICE_UNIT_FILE"
# Create the service unit file with new configuration
echo "$SERVICE_UNIT" | sudo tee "$SERVICE_UNIT_FILE"
# Reload systemd to recognize the new/updated service
sudo systemctl daemon-reload
# Enable the service to start on boot and start it immediately
sudo systemctl enable escuplast-service
sudo systemctl start escuplast-service
echo "Service updated and restarted successfully."
else
# Service file does not exist, create it with the specified configuration
echo "$SERVICE_UNIT" | sudo tee "$SERVICE_UNIT_FILE"
# Reload systemd to recognize the new service
sudo systemctl daemon-reload
# Enable the service to start on boot and start it immediately
sudo systemctl enable escuplast-service
sudo systemctl start escuplast-service
echo "Service created and started successfully."
fi
# Optionally, check the service status
echo "Checking the service status..."
sudo systemctl status escuplast-service --no-pager