ubuntu, updated

Services: running, start, autostart

Check if a service is running, start and stop it, and enable or disable autostart at boot with systemctl.

The idea

systemd starts the system and keeps services (daemons) running. Each service is described by a unit file, e.g. ssh.service. You control them with systemctl. The .service suffix is optional: systemctl status nginx is enough.

A service has two independent states:

StateQuestionChange with
active / inactive / failedIs it running now?start, stop, restart
enabled / disabledDoes it start at boot?enable, disable

So enable alone does not start a service, and stop does not prevent it from starting at the next boot.

Is it running?

systemctl status nginx
● nginx.service - A high performance web server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled)
     Active: active (running) since Tue 2026-10-21 08:02:11 UTC; 2h ago
   Main PID: 1234 (nginx)

Short yes/no answers, handy in scripts:

systemctl is-active nginx       # active / inactive / failed
systemctl is-enabled nginx      # enabled / disabled / masked

Cheat sheet

WhatCommand
Start / stop / restartsudo systemctl start|stop|restart nginx
Reload config without dropping connectionssudo systemctl reload nginx
Autostart at bootsudo systemctl enable nginx
Autostart and start nowsudo systemctl enable --now nginx
No autostartsudo systemctl disable nginx
No autostart and stop nowsudo systemctl disable --now nginx
Block completely (can’t be started at all)sudo systemctl mask nginx (unmask to undo)
What failed?systemctl --failed
All running servicessystemctl list-units --type=service --state=running
All services with autostartsystemctl list-unit-files --type=service --state=enabled
Show the unit filesystemctl cat nginx
After editing unit filessudo systemctl daemon-reload
Logs of the servicejournalctl -u nginx -n 50 (more: Searching logs)

Change a service without touching the package file

sudo systemctl edit nginx

This creates an override in /etc/systemd/system/nginx.service.d/override.conf that survives package updates. Example: always restart the service if it crashes.

[Service]
Restart=on-failure
RestartSec=5

Your own service

To run your own program or script as a service (with autostart and restart on crash):

[Unit]
Description=My App
After=network-online.target
Wants=network-online.target

[Service]
User=myapp
ExecStart=/opt/myapp/bin/myapp
Restart=on-failure

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now myapp

WantedBy=multi-user.target is what makes enable work: it means “start during normal boot”. For a dedicated myapp user, see Permission best practices.

§ home
~/knubertus/home/
1,1 Top