Introduction to Systemd
Systemd manages Linux system resources through units. A unit can represent a service, target, mount point, timer, socket, or another system resource.
1. Unit file directories
Section titled “1. Unit file directories”Unit files are usually stored in one of these directories, ordered by priority:
/etc/systemd/system: custom system or user configuration./run/systemd/system: runtime-generated configuration./usr/lib/systemd/system: unit files installed by the system or third-party software.
Systemd reads many default unit files from /usr/lib/systemd/system/, while /etc/systemd/system/ often contains custom files or symbolic links.
2. Unit and target
Section titled “2. Unit and target”A Unit is the basic object managed by Systemd. A Target is a group of units, similar to a SysV-init runlevel.
Common service unit sections:
[Unit]: description and dependency metadata.[Service]: service lifecycle and runtime behavior.[Install]: how the unit is enabled and attached to a target.
Example service unit:
[Unit]Description=Hello WorldAfter=docker.serviceRequires=docker.service[Service]TimeoutStartSec=0ExecStartPre=-/usr/bin/docker kill busybox1ExecStartPre=-/usr/bin/docker rm busybox1ExecStartPre=/usr/bin/docker pull busyboxExecStart=/usr/bin/docker run --name busybox1 busybox /bin/sh -cExecStop="/usr/bin/docker stop busybox1"ExecStopPost="/usr/bin/docker rm busybox1"[Install]WantedBy=multi-user.target3. Common systemctl commands
Section titled “3. Common systemctl commands”systemctl list-unitssystemctl list-units --allsystemctl list-units --failedsystemctl list-units --type=servicesystemctl status bluetooth.servicesudo systemctl start apache.servicesudo systemctl stop apache.servicesudo systemctl restart apache.servicesudo systemctl daemon-reloadsystemctl list-dependencies nginx.service4. Target management
Section titled “4. Target management”systemctl list-unit-files --type=targetsystemctl list-dependencies multi-user.targetsystemctl get-defaultsudo systemctl set-default multi-user.targetsudo systemctl isolate multi-user.target5. Logs
Section titled “5. Logs”Systemd uses Journald for logs. Use journalctl to inspect system and service logs.
[1]. https://cloud.tencent.com/developer/article/1516125