Appearance
Systemd might have been a controversial project among the more traditional UNIX community, but whether we like it or not it has been adopted by a number of mayor projects and support for it has grown over the years, proving that it's here to stay, so we might as well get used to it.
This is a reference for the most basic systemd commands, as well as some that are more situation yet still handy to be aware of.
Basic Stop/(re)start/reload commands ​
Starting a unit will make it "active". In the context of a service, this means the program is currently running.
bash
# Stop unit
systemctl stop <unitname>
# Start unit
systemctl start <unitname>
# Restart unit
systemctl restart <unitname>
# Restart unit
systemctl try-restart <unitname>
# Stop unit
systemctl stop <unitname>
bash
systemctl daemon-reload
systemctl kill <unitname>
Manage units that start at boot ​
Following the terminology of other init systems, to have a unit attempt to be started at boot, you use the minoker "enable/disable".
bash
# Enable the unit to be started at boot
systemctl enable <unitname>
# Disable the unit from being started at boot
systemctl disable <unitname>
Checking the status/log of a unit ​
bash
# show status and last few lines of log
systemctl status <unitname>
# show and follow (like tail -f) the logs for a unit
journalctl -fu <unitname>
# Below some commands that might be useful in scripts, but less informative than `systemctl status`
systemctl is-active <unitname>
systemctl is-enabled <unitname>
systemctl is-failed <unitname>
Listing unit files ​
bash
# List the dependency tree of active units along with the processes each unit started
systemctl status
# List all loaded units
systemctl list-units
# List only .service files
systemctl list-units --type=service
# List all available units (not just the ones loaded)
systemctl list-unit-files
Preventing a unit from running ​
You can "mask" a unit to prevent it from running at all, blocking attempts to start it, both manual and automatic (eg. when other services that have it as dependencies try to start).
Under the hood this is essentially linking the unit file to /dev/null
bash
# Mask the unit preventing it from activation
systemctl mask <unitname>
# Unmask a previously masked unit, allowing it's activation back again
systemctl unmask <unitname>
Managing targets ​
Targets are a special type of unit file that takes the place of runlevels from the traditional SysVInit system. However, they are a bit more flexible than run levels, as several targets can be active at the same time and be dependent to each other in the excution tree.
They are meant to be a synchronization point in the dependency chain during startup or to group a set of units that must be all finish their startup before the target is considered active.
bash
# See the default target (the one configured to run at startup)
systemctl get-default
# Change the default target (it won't be set active until next boot)
systemctl set-default <targetname>
# List active targets
systemctl list-units --type target --state active
# Output an svg file detailing the entire chain
systemd-analyze plot > initchain.svg