Custom Services
Creating and Configuring Custom Systemd Service Units¶
Example Service Unit File¶
Create a file at /etc/systemd/system/myapp.service with the following content:
[Unit]
Description=My Custom Application Service
After=network.target
[Service]
User=myappuser
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/gunicorn --bind 0.0.0.0:5000 app:app
Restart=on-failure
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=myapp
[Install]
WantedBy=multi-user.target
Step-by-Step Configuration¶
- Create the service file using
nanoorvim - Reload systemd with
sudo systemctl daemon-reload - Enable the service with
sudo systemctl enable myapp.service - Start the service with
sudo systemctl start myapp.service - Verify status with
systemctl status myapp.service
Example Use Case: Web Application¶
For a Python Flask app running as myappuser:
- Place the service file in /etc/systemd/system/myapp.service
- Set WorkingDirectory to the app's root directory
- Use ExecStart with gunicorn or uvicorn as the application server:
- ExecStart=/usr/bin/gunicorn --bind 0.0.0.0:5000 app:app
- ExecStart=/usr/bin/uvicorn app:app --host 0.0.0.0 --port 5000
- Configure StandardOutput and StandardError for logging
Best Practices Integration¶
- Naming: Use
myapp.servicefor clear identification - Permissions: Run as dedicated user (
myappuser) for security - Logging: Direct output to syslog with
SyslogIdentifier - Testing: Use
systemctl daemon-reloadfollowed bysystemctl status myapp.servicefor syntax validation - Restart Policy: Set
Restart=on-failurefor reliability