Most Linux distributions are now managed by systemd, which is becoming more and more complex, but there are only so many common operations, so today I will talk about my own common operations and configuration.
Operation
daemon-reload
When adding a new service to a system, it is common to keep changing the test.service configuration file. After changing the configuration file, it is usually necessary to run systemctl daemon-reload
to re-add the systemd configuration.
no-page
The service log is usually viewed using journalctl. However, when the log exceeds the maximum number of characters that can be displayed on the current line, the log is truncated by default, so you can use journalctl -u <service> --no-page
to have the log automatically folded.
disk-usage
The systemd journal configuration file is at /etc/systemd/journald.conf
, so if I want to see how much disk space a service’s journal is taking up, I can use the command journalctl -u <servie> --disk-usage
.
poweroff
There are many shutdown commands under Linux, init
, shutdown
, poweroff
, but if you have paid attention you will find the following facts.
|
|
Yes, systemd works better. systemd determines the $0
name to execute the corresponding command, so we can execute systemctl poweroff
to shut down the machine.
list-dependencies
systemd can specify the dependencies of a service, specified by the keywords After
, Before
, Requires
, and the specific dependency path of the service is displayed via systemctl list-dependencies <service>
.
Configuration
Restart
Most services are a resident process and we usually want to keep it running despite crashes, kills, abnormal interruptions, etc. So we can specify Restart=on-failure
in [Servie]
to accomplish this.
TimeoutStopSec
When stopping a service via systemctl stop
, if the action specified in ExecStop
takes a long time, we can set the timeout by adding TimeoutStopSec=10s
.
RefuseManualStop
If there are services that we just want to keep up, or start automatically through a dependency, and do not want human intervention, then we can ensure that the service cannot be stopped manually by setting RefuseManualStop=true
, e.g. rdma.service:.
PartOf
is similar to Requires=, except that it only acts on the stopping or restarting of a unit. The implication is that when a unit listed here is stopped or restarted, the unit itself is also stopped or restarted at the same time. Note that this dependency is unidirectional and that the stopping or restarting of the unit itself does not affect the units listed here.
If a.service contains PartOf=b.service, then this dependency will appear as ConsistsOf=a.service in the property list of b.service. That is, you cannot set the ConsistsOf= dependency directly.
@
Anyone who has configured openvpn on Linux will have seen examples of [openvpn@client.service](mailto:%60openvpn@client.service)
such as.
If you need to start OpenVPN automatically at system boot, you can configure it by enabling openvpn@.service on the corresponding machine for both server and client. For example, if the client configuration file is /etc/openvpn/client.conf, the service name should be openvpn@client.service. Or, if the server-side configuration file is /etc/openvpn/server.conf, the service name would be openvpn@server.service.
Let’s look at the corresponding systemd configuration file.
|
|