-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig_as_service.example.sh
More file actions
79 lines (56 loc) · 2.18 KB
/
Copy pathconfig_as_service.example.sh
File metadata and controls
79 lines (56 loc) · 2.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
#!/bin/bash
# === USER CONFIGURATION START ===
# Update these paths and values as needed
PROJECT_DIR="/home/pi/myproject" # Full path to your project directory
SERVICE_NAME="movieplayer" # Name for the systemd service (no spaces)
PYTHON_BIN="/usr/bin/python3" # Path to the Python interpreter
USER_NAME="pi" # The user who should run the service
# === USER CONFIGURATION END ===
LAUNCH_SCRIPT="$PROJECT_DIR/launch.sh"
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
echo "🔧 Step 1: Creating launch script at $LAUNCH_SCRIPT"
# Create a simple shell script to launch your Python app
cat > "$LAUNCH_SCRIPT" <<EOF
#!/bin/bash
# Launch script for the Movie Frame Player
# Change to the project directory
cd "$PROJECT_DIR"
# Run the main Python script with the --auto flag
$PYTHON_BIN movieplayer.py --auto
EOF
# Make sure the launch script is executable
chmod +x "$LAUNCH_SCRIPT"
echo "✅ launch.sh created and marked executable"
echo "🔧 Step 2: Creating systemd service file at $SERVICE_FILE"
# Create the systemd service file that tells Linux how to manage your script
sudo tee "$SERVICE_FILE" > /dev/null <<EOF
[Unit]
Description=Movie Frame Player Service
After=network.target
[Service]
# Path to the launch script created above
ExecStart=$LAUNCH_SCRIPT
# Where the service should run from
WorkingDirectory=$PROJECT_DIR
# Always restart if the script fails or crashes
Restart=always
# Which user should run the service
User=$USER_NAME
# Optional: set environment variables here
Environment=ENVIRONMENT=production
# Log output to the system journal
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
EOF
echo "✅ systemd service file created"
echo "🔄 Step 3: Reloading systemd daemon to pick up the new service"
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
echo "🚀 Step 4: Enabling and starting the $SERVICE_NAME service"
sudo systemctl enable "$SERVICE_NAME"
sudo systemctl start "$SERVICE_NAME"
echo "📋 Step 5: Showing the status of the new service"
sudo systemctl status "$SERVICE_NAME" --no-pager
echo "✅ Setup complete! Your script should now be running as a service."