Systemctl

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the unix category.

Last Updated: 2024-11-23

Systemctl is the interface to systemd.

Get state of all services

Figure out how each of the myriad systems that boot with your machine has run (e.g. "successfully", "with errors", "timeouts", etc.)

systemctl

e.g. returns

UNIT                      LOAD   ACTIVE SUB       DESCRIPTION
snap-core-9804.mount      loaded active mounted   Mount unit for core, revision 9804
snap-core-9993.mount      loaded active mounted   Mount unit for core, revision 9993
var-lib-lxcfs.mount       loaded active mounted   /var/lib/lxcfs
acpid.path                loaded active waiting   ACPI Events Check
init.scope                loaded active running   System and Service Manager
lvm2-monitor.service      loaded active exited    Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or progre
lxcfs.service             loaded active running   FUSE filesystem for LXC
lxd-containers.service    loaded active exited    LXD - container startup/shutdown
monit.service             loaded active running   LSB: service and resource monitoring daemon
nginx.service             loaded active running   A high performance web server and a reverse proxy server

This can be used in place of the old-fashioned way of checking boot messages. What's more, you can get even more info about a failed service (original PID, error code etc.) with the following:

# systemctl status UNIT
systemctl status lxd-containers.service

 lxd-containers.service - LXD - container startup/shutdown
   Loaded: loaded (/lib/systemd/system/lxd-containers.service; enabled; vendor preset: enabled)
   Active: active (exited) since Wed 2019-06-26 09:10:07 UTC; 1 years 2 months ago
     Docs: man:lxd(1)
 Main PID: 635 (code=exited, status=0/SUCCESS)
    Tasks: 0 (limit: 2319)
   CGroup: /system.slice/lxd-containers.service

Start a service

sudo systemctl enable monit

Restart a service

sudo systemctl restart monit

Remove an unwanted service:

See document on systemd

Reload editing service files

sudo systemctl daemon-reload

This gets the system to pick up changes. Now you can restart an individual service with systemctl restart SERVICE

Resources