Unit : entity managed by systemd.
- /lib/systemd/system : systemd standard default units
- /usr/lib/systemd/system : maintainer, locally installed packages
- /run/systemd/system : transient unit files used at runtime
- /run/systemd/generator : place where systemd generates units automagically (eg. mount units based on /etc/fstab, cf man systemd.mount)
- /etc/systemd/system : admin custom unit files
Nice place to put service startup script ? /usr/local/sbin
systemd-delta : identify and compare overriding unit files
Helpful man pages : systemd.exec, systemd.mount, systemd.service
$ systemctl -t help
Available unit types:
service
socket
busname
target
snapshot
device
mount
automount
swap
timer
path
slice
scope$ systemctl [list-units]
UNIT LOAD ACTIVE SUB DESCRIPTION
proc-sys-fs-binfmt_misc.automount loaded active running Arbitrary Executable File Formats File System Automount Point
sys-devices-pci0000:00-0000:00:07.1-ata1-host1-target1:0:0-1:0:0:0-block-sr0.device loaded active plugged VMware_Virtual_IDE_CDROM_Drive
sys-devices-pci0000:00-0000:00:15.0-0000:03:00.0-host0-target0:0:0-0:0:0:0-block-sda-sda1.device loaded active plugged Virtual_disk 1
sys-devices-pci0000:00-0000:00:15.0-0000:03:00.0-host0-target0:0:0-0:0:0:0-block-sda-sda2.device loaded active plugged Virtual_disk 2
...
boot.mount loaded active mounted /boot
dev-hugepages.mount loaded active mounted Huge Pages File System
dev-mqueue.mount loaded active mounted POSIX Message Queue File System
etc-apache2.mount loaded active mounted /etc/apache2
exploit.mount loaded active mounted /exploit
home.mount loaded active mounted /home
opt-forge.mount loaded active mounted /opt/forge
opt.mount loaded active mounted /opt
proc-sys-fs-binfmt_misc.mount loaded active mounted Arbitrary Executable File Formats File System
run-user-0.mount loaded active mounted /run/user/0
run-user-491.mount loaded active mounted /run/user/491
run-user-6010.mount loaded active mounted /run/user/6010
run-user-61119.mount loaded active mounted /run/user/61119
run-user-61282.mount loaded active mounted /run/user/61282
# Filtering
$ systemctl --type=service
$ systemctl -t service
$ systemctl list-units --type=service --all$ systemctl list-unit-files -t service$ systemctl --failed$ systemctl status
● my-server
State: degraded
Jobs: 0 queued
Failed: 1 units
Since: Sat 2022-04-30 20:45:29 CEST; 1 months 0 days ago
CGroup: /
├─1 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
├─user.slice
│ ├─user-491.slice
│ │ ├─session-9907.scope
│ │ │ ├─ 7151 systemctl status
│ │ │ ├─ 7152 less$ systemctl enable --now httpd mariadbsystemctl -H [hostname] restart httpd$ systemctl get-default# Show default and overrides aggregate
$ systemctl cat httpd# List unit's properties
$ systemctl show --all httpd
# Query single property
$ systemctl show -p Restart httpd# Create directory
$ mkdir /etc/systemd/system/[name.type.d]
$ Create the drop-in
$ vi /etc/systemd/system/httpd.service.d/50-httpd.conf
[Service]
Restart=always
# Notify systemd of the changes
$ systemctl daemon-reload# Create the drop-in
$ systemctl edit httpd
[Service]
Restart=always
# Changes take effect upon writing the file
$ systemctl show -p Restart httpd
Restart=alwaysTip : pass --full to create a copy of the original unit file
$ sudo systemctl enable my-startup.service
Created symlink /etc/systemd/system/multi-user.target.wants/my-startup.service -> /etc/systemd/system/my-startup.service$ vi /etc/systemd/system/my-service.service
[Unit]
Description=A simple service
After=network.target
[Service]
ExecStart=/usr/local/bin/my-service
[Install]
WantedBy=multi-user.target$ systemd-analyze verify service_name