Sub topic 6
Running Prometheus Components as systemd Services
This chapter assumes that you are comfortable with the Linux command line, systemd, YAML files, and TCP ports. It also assumes that the required binaries are already installed. The examples use /usr/local/bin for binaries and run exporters without root privileges.
Why use systemd
A systemd service gives a monitoring component a controlled lifecycle. systemd can:
- Start the component during boot.
- Restart it after an unexpected failure.
- Record its output in the journal.
- Run it as a dedicated, unprivileged user.
- Report whether it is active and listening.
Keep each component in its own service. This makes failures easier to isolate.
| Component | Typical service name | Common port |
|---|---|---|
| node_exporter | `node_exporter.service` | 9100 |
| Prometheus | `prometheus.service` | 9090 |
| Alertmanager | `alertmanager.service` | 9093 |
| Grafana | `grafana-server.service` | 3000 |
Create an exporter service
Create a system user and a unit file:
sudo useradd --system --no-create-home --shell /usr/sbin/nologin node_exporter
sudo install -d -o node_exporter -g node_exporter /var/lib/node_exporter
sudoedit /etc/systemd/system/node_exporter.serviceUse this unit:
[Unit]
Description=Prometheus Node Exporter
After=network-online.target
Wants=network-online.target
[Service]
User=node_exporter
Group=node_exporter
ExecStart=/usr/local/bin/node_exporter
Restart=on-failure
RestartSec=5s
NoNewPrivileges=true
[Install]
WantedBy=multi-user.targetExecStart must point to the actual executable. Restart=on-failure handles crashes, but it does not fix configuration errors. NoNewPrivileges prevents the process from gaining additional privileges.
Load, start, and inspect the service
After creating or changing a unit, make systemd reread its files:
sudo systemctl daemon-reload
sudo systemctl enable --now node_exporter.service
sudo systemctl status node_exporter.serviceenable makes the service start at boot. --now starts it immediately. Confirm that the exporter is listening:
curl http://127.0.0.1:9100/metricsView recent logs with:
sudo journalctl -u node_exporter.service -n 50 --no-pagerThe same lifecycle applies to Prometheus and Alertmanager. Their units usually add configuration arguments, such as --config.file=/etc/prometheus/prometheus.yml. Grafana is commonly installed with a vendor-provided unit, so inspect its existing unit before replacing it.
Common failures
203/EXEC:ExecStartpoints to a missing file or a file without execute permission.- Immediate restart loops: inspect
journalctl; the binary may reject an option or configuration file. - Port already in use: find the conflicting process with
sudo ss -ltnp | grep ':9100'. - Service starts but cannot be scraped: check the listening address, firewall rules, and the target's hostname or IP address.
- Changes have no effect: run
systemctl daemon-reloadafter changing the unit.
Exercise
What commands reload a changed unit file and restart node_exporter while preserving its boot-time enablement?
Show answer
Run sudo systemctl daemon-reload followed by sudo systemctl restart node_exporter.service. The service remains enabled unless you disable it.