← Full reference

service and systemctl

Starting, stopping and checking services — systemctl on a regular server, service and init scripts in a container

A service (daemon) is a program that runs in the background: nginx, sshd, cron, postgres. On a regular Ubuntu server they are managed by systemd via the systemctl command. In a container systemd usually isn't running — there the old service wrapper works, calling scripts from /etc/init.d/.

What works where

EnvironmentWhat to manage withHow to tell
Server or VM with systemdsystemctl, logs — journalctlcat /proc/1/comm → systemd
Container without systemd (like the sandbox)service name action or /etc/init.d/name actioncat /proc/1/comm → bash, sh, etc.
Container with supervisordsupervisorctla supervisord process in ps

In the sandbox systemctl is installed but doesn't work:

systemctl status cron
# System has not been booted with systemd as init system (PID 1). Can't operate.
# Failed to connect to bus: Host is down

On a machine with systemd, the service command forwards the call to systemctl itself, so service nginx restart works there too.

service

service name action
service --status-all
CommandWhat it does
service name start / stopstart / stop
service name restartstop and start again
service name reloadre-read the config without stopping (if the service supports it)
service name force-reloadreload, or restart if that isn't supported
service name statusis it running; code 0 — running, 3 — not running
service nameno action — a hint listing the supported actions
service --status-allall init scripts: [ + ] — running, [ - ] — not, [ ? ] — the script doesn't support status

The list of actions depends on the script: nginx also has configtest, rotate, upgrade. /etc/init.d/name action does the same thing directly.

systemctl (on a server with systemd)

CommandWhat it does
systemctl start / stop / restart unitstart / stop / restart now
systemctl reload unitre-read the config without restarting
systemctl reload-or-restart unitreload if supported, otherwise restart
systemctl status unitstate, PID, memory, last log lines
systemctl enable unit / disableenable / disable starting at boot (does not start it now)
systemctl enable --now unitenable starting at boot and start right away
systemctl is-active / is-enabled / is-failed unita short answer and an exit code — for scripts
systemctl mask / unmask unitforbid starting entirely, even manually and as a dependency
systemctl daemon-reloadre-read unit files after editing them
systemctl cat unitshow the unit file with all drop-ins
systemctl edit unitcreate an override file (/etc/systemd/system/unit.d/override.conf)
systemctl list-units --type=serviceloaded services and their state
systemctl --failedfailed units
systemctl list-timerstimers — systemd's replacement for cron
journalctl -u unitservice log; -f — follow, -n 50 — last 50 lines, --since "1 hour ago"

The unit name can be written without .service: systemctl restart nginx.

States in systemctl status: active (running) — running, inactive (dead) — stopped, failed — crashed or exited with an error, activating — starting. The Loaded: line shows the path to the unit file and enabled/disabled — whether starting at boot is enabled.

supervisorctl

CommandWhat it does
supervisorctl statusall programs and their state (RUNNING, STOPPED, FATAL, BACKOFF)
supervisorctl start / stop / restart namemanage a program; all — all of them
supervisorctl rereadre-read configs and show what changed
supervisorctl updateapply changes: start new programs, restart changed ones
supervisorctl tail -f namea program's output

Program configs live in /etc/supervisor/conf.d/*.conf (a [program:name] section), and supervisord itself is started with service supervisor start.

Examples

# what's there at all and what's running
service --status-all

# start cron and check it
service cron start
service cron status

# test the nginx config and apply it gracefully
service nginx configtest && service nginx reload

# in a script: is the service running
if service nginx status >/dev/null; then echo "running"; fi

# directly via the init script
/etc/init.d/ssh restart

# supervisord: add a program and apply
service supervisor start
supervisorctl reread && supervisorctl update
supervisorctl status

# on a server with systemd
systemctl enable --now nginx
systemctl status nginx
journalctl -u nginx -n 50 --no-pager
systemctl daemon-reload && systemctl restart myapp

Pitfalls

  • systemctl doesn't work in a container, even if installed: it needs systemd as PID 1. The error "System has not been booted with systemd" isn't a broken service but a signal to use service.
  • enable doesn't start the service, and start doesn't enable it at boot. On a server you usually need both after installation: systemctl enable --now.
  • After editing a unit file you need daemon-reload, otherwise systemd keeps using the old version.
  • reload doesn't re-read everything. For example, some daemons apply a change of port or user only after restart.
  • There's no autostart in a container. service x start lasts until the container restarts; nobody reads the links in /etc/rc*.d.
  • service … status prints differently in different scripts. In scripts rely on the exit code, not the text.
  • If a service won't start, look at its log first: /var/log/<service>/… in a container, journalctl -u on a server. A common cause is a config error or a port already in use (ss -ltnp).

See also

kill · ps · cron · nginx · ss