Abstract
- An Init System that manages the lifecycles of of Process (进程) for OS that is based on Linux Kernel
Systemd Unit
- System resources that Systemd manages
Systemd Unit vs Systemd Service
A Systemd Service is a specific type of unit that represents a system service or daemon. Systemd Unit covers a bigger scope that includes Socket, Mounted File System and IO Device etc.
Systemd Unit management cheatsheet
Refer to this How To Use Systemctl to Manage Systemd Services and Units | DigitalOcean to list, edit and inspect Systemd Unit which is used to manage the Systemd.
Just a side note,
systemctl start
starts the systemd unit immediately but the system unit isn’t auto started after a reboot.systemctl enable
ensures the systemd unit is restarted after a reboot, however it is only started after a reboot.
View the systemd unit configuration
systemctl list-units
to find the systemd unit you interested in. Then usesystemctl cat <systemd_unit>
to view the configuration.
Static Systemd Runtime Unit
- Systemd Unit stored inside
/lib/systemd/system
, provided by the OS or packages
Dynamic Systemd Runtime Unit
- Systemd Unit stored inside
/run/systemd/system
, reflect the current state of systemd-managed services and resources
Custom Systemd Unit
- We should place our own Systemd Unit files in
/etc/systemd/system/
. This can override or extend the default unit files found in Static Systemd Runtime Unit without directly modifying the default unit files provided by the OS or packages.
Important
Systemd caches the systemd config files. When we make edits or add in new systemd config files. We should call
systemctl daemon-reload
to take in the changes.
Example
Here are some example: Shadowsocks service & HashiCorp Vault service
Systemd Log
- You can use
journalctl -u <systemd_unit>
to view all the log produced by a Systemd Unit. Refer to How To Use Journalctl to View and Manipulate Systemd Logs | DigitalOcean for more details
Systemd Log export & backup
You can use
journalctl -u shadowsocks --since "2024-04-12 00:00:00" --until "2024-04-13 00:00:00" -o cat > shadowsocks_2024-04-12.log
to export the logs of a system unit collected from2024-04-12 00:00:00
to2024-04-13 00:00:00
into a file calledshadowsocks_2024-04-12.log
. The you can use RCLONE to backup the file to a cloud storage.