Interview Prep/Linux/Processes, services and systemd

Your team has a Python API that someone starts with `nohup python app.py &`. Make it a proper service running as a non-root user.

BeginnerScenarioProcesses, services and systemd
Answer

Situation read → Turn an ad-hoc process into a managed, restartable systemd unit.

What I'd do →

  1. sudo useradd --system --shell /usr/sbin/nologin --home /opt/api api
  2. Create /etc/systemd/system/api.service:
[Unit]
Description=Orders API
After=network-online.target
Wants=network-online.target

[Service]
User=api
Group=api
WorkingDirectory=/opt/api
EnvironmentFile=/etc/api/api.env
ExecStart=/opt/api/venv/bin/gunicorn -b 0.0.0.0:8000 app:app
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
  1. sudo systemctl daemon-reload && sudo systemctl enable --now api
  2. Check systemctl status api and journalctl -u api -f.

Why → systemd restarts on crash, starts at boot, captures logs and runs it as a locked-down user.

Interview tip → Mention Restart=on-failure vs always, and that ExecStart needs an absolute path.

All Linux questions Roadmap: Linux

More on Processes, services and systemd