https://access.redhat.com/solutions/3120581
SOLUTION 已验证 - 已更新 2017年七月20日15:22 -
环境
- Red Hat Enterprise Linux 7
- systemd
问题
After=/Before=options don't guarantee the listed units has been started and running. How do I write a service unit file which enforces that particular services have to be started?
决议
Background:
On RHEL7, a service unit file needs to specify its dependencies using After=/Before= options. This ensures that (if configured that way) that, for example, b.service will be started after a.service. What it does not do is ensure that b.service is only started if a.service is successfully started. This is because the After=/Before= options just specify the order of dependencies. Any After=/Before= does not care about if the dependency is running or not.
If a service requires that the dependent services have to be running, you need to consider the options listed below to satisfy that requirement.
Detailed information:
A [Unit] section of a service configuration file can contain following options which enforce a requirement that the listed units must be started and running. The details are quoted from man 5 systemd.unit.
Requires=
Requisite=
PartOf=
-
The details of
Requires=
This option configures requirement dependencies on other units. If this unit gets activated, the units listed here will be activated as well. If one of the other units gets deactivated or its activation fails, this unit will be deactivated. This option may be specified more than once or multiple space-separated units may be specified in one option in which case requirement dependencies for all listed names will be created. Note that requirement dependencies do not influence the order in which services are started or stopped. This has to be configured independently with theAfter=orBefore=options. If a unitfoo.servicerequires a unitbar.serviceas configured withRequires=and no ordering is configured withAfter=orBefore=, then both units will be started simultaneously and without any delay between them iffoo.serviceis activated. Often it is a better choice to useWants=instead ofRequires=in order to achieve a system that is more robust when dealing with failing services. -
The details of
Requisite=
This option is similar toRequires=respectively. However, if the units listed here are not started already, they will not be started and the transaction will fail immediately. -
The details of
PartOf=
This option configures dependencies similar toRequires=, but limited to stopping and restarting of units. When systemd stops or restarts the units listed here, the action is propagated to this unit. Note that this is a one-way dependency — changes to this unit do not affect the listed units.
How do I choose which option preferred to my unit file?
This section provides some guidance on how to choose the appropriate keyword(s) in your unit file(s). In the following, we assume a.service and b.service have their dependencies as described below.
a.service (Before=b.service)
b.service (After=a.service)
-
When
b.serviceis activated,a.serviceshould be activated also (if not already). The unita.servicemust start successfully - if it fails or is deactivatedb.servicewill also be deactivated.
You should useRequires=to activatea.serviceand ensureb.servicecannot start unlessa.servicestarts. -
If
a.serviceis not started already,b.serviceshould not be started and will fail immediately. The activation ofa.serviceshould not be triggered by the activation ofb.service.
You should useRequisite=to fulfill the requirement. -
b.servicealways needs to be activated aftera.serviceis activated. Buta.serviceshould not be activated whenb.serviceis activated howevera.servicedoes need to be stopped or restarted whenb.serviceis stopped or restarted.
You should usePartOf=in this situation.You may need to use multiple keywords to achieve a desired outcome. For example, you may have
b.servicewithRequires=a.servicesob.servicewill not start unlessa.servicestarts and startingb.servicestartsa.servicefirst. As well in the definition ofa.serviceyou might needPartOf=b.serviceso ifb.serviceis stopped or restarteda.servicewill have the same action taken on it.Complex interactions between units can be achieved but requires you to plan out what interactions are required. For other options, please refer to
man 5 systemd.unit.

本文详细介绍了在Red Hat Enterprise Linux 7中,如何使用systemd的单元文件配置选项来确保服务之间的正确依赖关系。文章解释了After=/Before=选项的作用及限制,并提供了Requires=、Requisite=和PartOf=等选项的深入解析,帮助管理员更好地控制服务启动顺序和依赖状态。
1万+

被折叠的 条评论
为什么被折叠?



