Managing Services with Systemd
Systemd is the modern standard for managing system daemons/services. It has widely replaced
other startup sequences and runlevels. It is the first PID
to start, and logs events to journald.
Terminology
- systemd - daemon
- systemctl - control action
- target - collection of services
Getting Info
sudo systemctl list-units --all
- display all units filessudo systemctl list-unit-files --type=service
- all services and current statussudo systemctl list-dependencies <service>
- show dependencies for a servicesudo systemctl mask <service>
- masking "turns off" a servicemask
/unmask
- deal with conflicting services
- services that use the same port etc.
Basic Usage
General
Systemd can be used to perform high-level system tasks.
default
- Enter system default moderescue
- Enter system rescue modeemergency
- Enter system emergency modehalt
- Shut down and halt the systempoweroff
- Shut down and poweroff systemreboot [ARG]
- Shut down and reboot systemsuspend
- Suspend the systemhibernate
- Hibernate the systemhybrid-sleep
- Hibernate and suspend system
Actions
sudo systemctl <ACTION> <SERVICE>
The following actions are available:
start
- start a stopped (or previously failed) svcstop
- stop a running svcrestart
- turn a svc all the off and back onreload
- quickly re-read new config (without turning off)enable
- set a service to start on bootdisable
- prevent a service starting on boot
For example, let's restart the Elasticsearch service:
-
sudo systemctl restart elasticsearch
● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2019-08-01 21:26:30 UTC; 1 weeks 5 days ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 6105 (sshd) CGroup: /system.slice/sshd.service └─6105 /usr/sbin/sshd -D Aug 01 21:26:30 es1 systemd[1]: Starting OpenSSH server daemon... Aug 01 21:26:30 es1 sshd[6105]: Server listening on 0.0.0.0 port 22. Aug 01 21:26:30 es1 sshd[6105]: Server listening on :: port 22. Aug 01 21:26:30 es1 systemd[1]: Started OpenSSH server daemon.
- enabled - set to start at boot
- active - current duration
- docs - documentation sources
- PID - process #