Skip to content

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

  1. Create the service file using nano or vim
  2. Reload systemd with sudo systemctl daemon-reload
  3. Enable the service with sudo systemctl enable myapp.service
  4. Start the service with sudo systemctl start myapp.service
  5. 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.service for clear identification
  • Permissions: Run as dedicated user (myappuser) for security
  • Logging: Direct output to syslog with SyslogIdentifier
  • Testing: Use systemctl daemon-reload followed by systemctl status myapp.service for syntax validation
  • Restart Policy: Set Restart=on-failure for reliability