Systemd is an init system and system manager for Linux machines.

In systemd, the target of most actions are "units", which are resources that systemd knows how to manage. Units are categorized by the type of resource they represent and they are defined with files known as unit files. The type of each unit can be inferred from the suffix on the end of the file.

List units:

systemctl # list active units systemctl --all # list all services systemctl --type=service systemctl --state=failed

States:

  • inactive
  • active: start succeed
  • failed: start failed

List unit files:

systemctl list-unit-files systemctl list-unit-files --type=service systemctl list-unit-files --state=enabled

States:

  • enabled: start at boot.
  • disabled: not start at boot.
  • masked: cannot be started.
  • static: cannot be enabled.

Unit file management:

systemctl cat nginx.service systemctl mask nginx.service # make it unstartable systemctl unmask nginx.service

Editing unit file:

Create a snippet in a directory (default /etc/systemd/system/nginx.service.d/*), override the original configuration file:

systemctl edit nginx.service

To undo the modification:

rm -r /etc/systemd/system/nginx.service.d

Create a copy in /etc/systemd/system/nginx.service, take place of the original configuration file (default /lib/systemd/system/nginx.service):

systemctl edit --full nginx.service

To undo the modification:

rm /etc/systemd/system/nginx.service

After modification, run the following command to reload configuration:

systemctl daemon-reload

Service management:

systemctl status nginx systemctl is-active nginx systemctl is-enabled nginx systemctl is-failed nginx

Operations:

systemctl start nginx.service # start nginx service systemctl start nginx # .service suffix can be left off systemctl stop nginx systemctl restart nginx systemctl reload nginx # reload configuration files without restarting systemctl enable nginx # start nginx automatically at boot systemctl disable nginx

How To Use Systemctl to Manage Systemd Services and Units - DigitalOcean