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
| Environment | What to manage with | How to tell |
|---|---|---|
| Server or VM with systemd | systemctl, logs — journalctl | cat /proc/1/comm → systemd |
| Container without systemd (like the sandbox) | service name action or /etc/init.d/name action | cat /proc/1/comm → bash, sh, etc. |
| Container with supervisord | supervisorctl | a 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
| Command | What it does |
|---|---|
service name start / stop | start / stop |
service name restart | stop and start again |
service name reload | re-read the config without stopping (if the service supports it) |
service name force-reload | reload, or restart if that isn't supported |
service name status | is it running; code 0 — running, 3 — not running |
service name | no action — a hint listing the supported actions |
service --status-all | all 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)
| Command | What it does |
|---|---|
systemctl start / stop / restart unit | start / stop / restart now |
systemctl reload unit | re-read the config without restarting |
systemctl reload-or-restart unit | reload if supported, otherwise restart |
systemctl status unit | state, PID, memory, last log lines |
systemctl enable unit / disable | enable / disable starting at boot (does not start it now) |
systemctl enable --now unit | enable starting at boot and start right away |
systemctl is-active / is-enabled / is-failed unit | a short answer and an exit code — for scripts |
systemctl mask / unmask unit | forbid starting entirely, even manually and as a dependency |
systemctl daemon-reload | re-read unit files after editing them |
systemctl cat unit | show the unit file with all drop-ins |
systemctl edit unit | create an override file (/etc/systemd/system/unit.d/override.conf) |
systemctl list-units --type=service | loaded services and their state |
systemctl --failed | failed units |
systemctl list-timers | timers — systemd's replacement for cron |
journalctl -u unit | service 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
| Command | What it does |
|---|---|
supervisorctl status | all programs and their state (RUNNING, STOPPED, FATAL, BACKOFF) |
supervisorctl start / stop / restart name | manage a program; all — all of them |
supervisorctl reread | re-read configs and show what changed |
supervisorctl update | apply changes: start new programs, restart changed ones |
supervisorctl tail -f name | a 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
systemctldoesn'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 useservice.enabledoesn't start the service, andstartdoesn'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. reloaddoesn't re-read everything. For example, some daemons apply a change of port or user only afterrestart.- There's no autostart in a container.
service x startlasts until the container restarts; nobody reads the links in/etc/rc*.d. service … statusprints 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 -uon a server. A common cause is a config error or a port already in use (ss -ltnp).