Version
Different version may have different syntax and options in unit file, check it first:
1 | systemctl --version |
To see systemd service unit configuration:
1 | man 5 systemd.service |
Systemd
How To Use Systemctl to Manage Systemd Services and Units
History:
1 | SysV Init -> Upstart -> Systemd |
The systemd
, system and service manager, is an init system used to bootstrap
the user space and to manage system processes after boot. Use systemctl
command to manage the service on a systemd enabled system.
The fundamental purpose of an init system is to initialize the components that must be started after the Linux kernel is booted (traditionally known as “userland” components). The init system is also used to manage services and daemons for the server at any point while the system is running.
Main commands, for example using nginx, some commands have to use sudo
if you
are non-root user, since it will affect te state of the operating system, you
can leave off the .service
suffix in command.
1 | # start on boot |
enable
will create a soft link into the location on disk where systemd looks
for autostart files (usually /etc/systemd/system/some_target.target.wants
).
The exit code can be used for shell script:
1 | # may need sudo |
Check system states managed by systemd:
1 | # list enabled units only |
list-unit-files
state column: The state will usually be enabled
, disabled
,
static
, or masked
. In this context, static
means that the unit file does
not contain an [Install]
section, which is used to enable a unit. As such,
these units cannot be enabled. Usually, this means that the unit performs a
one-off action or is used only as a dependency of another unit and should not be
run by itself.
1 | # after mask, cannot enable or start service |
To see full content and path of a unit file, vanilla unit files(don’t touch
them, override them if needed in other places, for example /etc/systemd/system
)
are in /usr/lib/systemd/system
and customized are in /etc/systemd/system
folder:
1 | # show unit file content and path |
You can also further override by creating override.conf file in
/etc/systemd/system/xxx.service.d
folder, there are some principles for
overriding, see here.
In systemd, service and other unit files can be tied to a target
.
Targets are special unit files that describe a system state or synchronization
point. Like other units, the files that define targets can be identified by
their suffix, which in this case is .target
. Targets do not do much by
themselves, but are instead used to group other units together.
This can be used in order to bring the system to certain states, much like other
init systems use runlevels
. (仍然可以显示系统的runlevel的)
For instance, there is a swap.target
that is used to indicate that swap is
ready for use. Units that are part of this process can sync with this target by
indicating in their configuration that they are WantedBy= or RequiredBy= the
swap.target. Units that require swap to be available can specify this condition
using the Wants=, Requires=, and After= specifications to indicate the nature of
their relationship.
1 | # all available targets |
Unlike runlevels, multiple targets can be active at one time. An active target indicates that systemd has attempted to start all of the units tied to the target and has not tried to tear them down again.
1 | # show all active targets |
This is similar to changing the runlevel in other init systems. For instance,
if you are operating in a graphical environment with graphical.target
active,
you can shutdown the graphical system and put the system into a multi-user
command line state by isolating the multi-user.target
. Since graphical.target
depends on multi-user.target but not the other way around, all of the graphical
units will be stopped.
1 | # check units that will be kept alive |
Stopping and rebooting system, note shutdown
, reboot
, poweroff
are
actually softlink to systemctl!
1 | sudo systemctl poweroff |
These all alert logged in users that the event is occurring.
有意思的是,这些都是同一个softlink,但是调用却有不同的效果呢? 利用了$0
作为判断, see
here.
1 | lrwxrwxrwx. 1 root root 16 Jun 6 2020 halt -> ../bin/systemctl |
Systemd Journal
How To Use Journalctl to View and Manipulate Systemd Logs
The journal is implemented with the journald
daemon, which handles all of the
messages produced by the kernel, initrd, services, etc.
这里介绍了关于systemd service journal的使用,更详细的介绍Journal 可以参考我的blog
<<Linux System Admin>>
.
Set time of the prompt:
1 | # available time zone |
To see log of a specific service:
1 | # full log |
Service Unit File
Understanding Systemd Units and Unit Files
Here only focus on .service
unit. Systemd manages a broad range of resources,
such as .target
, .socket
, .device
, .mount
, .swap
, etc. They may have
different section blocks.
Section block names are case-sensitive. Non-standard section name [X-name]
has
a X-
prefix.
1 | [Unit] |
key=value pairs 不止这些,遇到新的可以补充。
Systemd does not use a shell to execute commands and does not perform $PATH
lookup, so for example in ExecStartPre, must specify shell context to run
command:
1 | ExecStartPre=/bin/sh -c 'pgrep process_name > /var/run/process_name.pid' |
And because of no $PATH lookup, must use absolute path to binary or executable.
Setting PermissionsStartOnly=true
means that User & Group are only applied to
ExecStart. So switching to the new syntax will be :
1 | ExecStartPre=+/bin/bash -c '/bin/journalctl -b -u ntpdate | /bin/grep -q -e "adjust time server" -e "step time server"' |
Prefix with +
will be executed with higher privilege as root. Prefix -
means
if any of those commands fail, the remaining exec will not be interrupted,
otherwise the unit is considered failed.
Also note that per ExecStartPre
is running in isolation.
If needs to check the log of latest daemon start process, this will show you all
log instead of only unit, I found if use -u
to specify unit, some log will
miss:
1 | # use `b` and `f` to move page |
Systemd with multiple execStart: serivce type 和 ExecStart 个数有关.