| 1 |
# Start/Stop ordering and Dependencies |
| 2 |
|
| 3 |
There is a lot of confusion what the various definitions |
| 4 |
for dependencies After/Wants/Requires will actually do. |
| 5 |
|
| 6 |
Note that the list of Requires/etc services comes from |
| 7 |
multiple Requires/etc text-lines and on each text-line |
| 8 |
one may define multiple names of dependencies. |
| 9 |
|
| 10 |
The entry point for reading should be: |
| 11 |
|
| 12 |
https://www.freedesktop.org/software/systemd/man/systemd.unit.html#Requires= |
| 13 |
|
| 14 |
## Dependencies |
| 15 |
|
| 16 |
### Requires |
| 17 |
|
| 18 |
It says >>Configures requirement dependencies on other units. If this unit |
| 19 |
gets activated, the units listed here will be activated as well.<< / >>Note |
| 20 |
that requirement dependencies do not influence the order in which services |
| 21 |
are started or stopped. This has to be configured independently with the |
| 22 |
After= or Before= options.<< |
| 23 |
|
| 24 |
So actually, if there is a Requires but no After then the current service |
| 25 |
and its required dependency service may be started in parallel. |
| 26 |
|
| 27 |
Additionally >>If one of the other [Requires] units gets deactivated or its |
| 28 |
activation fails, this unit will be deactivated.<< / >>Often, it is a |
| 29 |
better choice to use Wants= instead of Requires= in order to achieve a system |
| 30 |
that is more robust when dealing with failing services.<< |
| 31 |
|
| 32 |
So theoretically, if we have a phpadmin service then if there is a call to |
| 33 |
"systemctl stop mariadb" then it should automatically do some "systemctl |
| 34 |
stop phpadmin". However if the mariadb-service fails and it just goes into |
| 35 |
an inactive-state then the phpadmin service continues to run. >>Use the |
| 36 |
BindsTo= dependency type together with After= to ensure that a unit may |
| 37 |
never be in active state without a specific other unit also in active |
| 38 |
state<< |
| 39 |
|
| 40 |
### Wants |
| 41 |
|
| 42 |
We have >>A weaker version of Requires=. Units listed in this option will |
| 43 |
be started if the configuring unit is. However, if the listed units fail |
| 44 |
to start or cannot be added to the transaction, this has no impact on the |
| 45 |
validity of the transaction as a whole. This is the recommended way to hook |
| 46 |
start-up of one unit to the start-up of another unit.<< |
| 47 |
|
| 48 |
So just like "Requires" when "systemctl start phpadmin" then it will also |
| 49 |
execute "systemctl start mariadb" (possibly in parallel). However mariadb |
| 50 |
might not be installed or it is "systemctl mask"ed, in which case there is |
| 51 |
not start-action attempted for it. |
| 52 |
|
| 53 |
### Requisite / BindsTo / PartOf / PropagateReloadTo |
| 54 |
|
| 55 |
For Bindsto we have >>When used in conjunction with After= on the same |
| 56 |
unit the behaviour of BindsTo= is even stronger. In this case, the unit |
| 57 |
bound to strictly has to be in active state for this unit to also be in |
| 58 |
active state.<< More importantly, this is completely transitive. |
| 59 |
|
| 60 |
For Requisite we have >>Similar to Requires=. However, if the units listed |
| 61 |
here are not started already, they will not be started and the transaction |
| 62 |
will fail immediately.<< In other words, "systemctl start phpadmin" will |
| 63 |
not run "systemctl start mariadb" but when mariadb is inactive then the |
| 64 |
call to start-phpadmin will fail. |
| 65 |
|
| 66 |
And for PartOf we have >>Configures dependencies similar to Requires=, but |
| 67 |
limited to stopping and restarting of units. When systemd stops or restarts |
| 68 |
the units listed here, the action is propagated to this unit.<< |
| 69 |
|
| 70 |
And for PropagatesReloadTo=, ReloadPropagatedFrom= we have >>Issuing a |
| 71 |
reload request on a unit will automatically also enqueue a reload request |
| 72 |
on all units that the reload request shall be propagated to via these two |
| 73 |
settings.<< |
| 74 |
|
| 75 |
### Conflicts |
| 76 |
|
| 77 |
This is an exclusive-mark, so if service A is started then it should |
| 78 |
stop service B and vice versa. |
| 79 |
|
| 80 |
## systemctl.py |
| 81 |
|
| 82 |
As there is no parallism in systemctl we can actually ignore anything that |
| 83 |
looks like After/Before. |
| 84 |
|
| 85 |
### start logic |
| 86 |
|
| 87 |
Without a global dependency graph we would do: |
| 88 |
|
| 89 |
GIVEN <phpadmin> Requires / BindsTo <mariadb> AND <mariadb> EXISTS. |
| 90 |
ON "start <phpadmin> THEN |
| 91 |
IF inactive <mariadb> THEN do "start <mariadb>" and wait for result. |
| 92 |
AND mark a "dependency-start <mariadb>" |
| 93 |
IF active <mariadb> THEN do not wait. |
| 94 |
AFTER wait IF active <mariadb> THEN do "start <phadmin>". BUT... |
| 95 |
AFTER wait IF inactive <mariadb> THEN fail "start <phpadmin>". |
| 96 |
|
| 97 |
GIVEN <phpadmin> Requires / BindsTo <mariadb> AND <mariadb> MISSING. |
| 98 |
ON "start <phpadmin>" THEN fail "start <phpadmin>". |
| 99 |
|
| 100 |
GIVEN <phpadmin> Wants <mariadb> AND <mariadb> EXISTS. |
| 101 |
ON "start <phpadmin> THEN |
| 102 |
: IF inactive <mariadb> THEN do "start <mariadb>" and wait for result. |
| 103 |
:: AND mark a "dependency-start <mariadb>" |
| 104 |
: IF active <mariadb> THEN do not wait. |
| 105 |
: AFTER wait IF active <mariadb> THEN do "start <phadmin>". |
| 106 |
: AFTER wait IF inactive <mariadb> THEN do "start <phpadmin>". |
| 107 |
|
| 108 |
GIVEN <phpadmin> Wants <mariadb> AND <mariadb> MISSING. |
| 109 |
ON "start <phpadmin>" THEN do "start <phpadmin>". |
| 110 |
|
| 111 |
GIVEN <phpadmin> Requisite <mariadb> AND <mariadb> EXISTS. |
| 112 |
ON "start <phpadmin> THEN |
| 113 |
: IF inactive <mariadb> THEN fail "start <phpadmin>" |
| 114 |
: IF active <mariadb> THEN do "start <phpadmin>" |
| 115 |
|
| 116 |
GIVEN <phpadmin> Conflicts <phpadmin5> AND <phpadmin5> EXISTS. |
| 117 |
ON "start <phpadmin>" THEN |
| 118 |
IF active <phpadmin5> THEN do "stop <phpadmin5>" |
| 119 |
|
| 120 |
### stop logic |
| 121 |
|
| 122 |
There is no explanatation whether a dependency that was |
| 123 |
indirectly started should also be stopped when the original |
| 124 |
script is being stopped. |
| 125 |
|
| 126 |
Without a global dependency graph we would do: |
| 127 |
|
| 128 |
GIVEN <phpadmin> Requires / BindsTo / Wants <mariadb> AND <mariadb> EXISTS. |
| 129 |
: IF active <mariadb> AND "dependency-start <mariadb>" |
| 130 |
: THEN do "stop <mariadb>" and drop dependency-start mark |
| 131 |
|
| 132 |
### restart logic |
| 133 |
|
| 134 |
Without a global dependency graph we would do: |
| 135 |
|
| 136 |
Nothing is propagated |
| 137 |
|
| 138 |
### reload logic |
| 139 |
|
| 140 |
Without a global dependency graph we would do: |
| 141 |
|
| 142 |
GIVEN <phpadmin> PropagatesTo <mariadb> AND <mariadb> EXISTS. |
| 143 |
: IF active <mariadb> THEN do "reload <mariadb>" |
| 144 |
|
| 145 |
## .wants directories |
| 146 |
|
| 147 |
The list of Wants may be extended by symlinks in a directory |
| 148 |
for the service file. So if there is a file 'phpadmin.service' |
| 149 |
then there MAY be a directory 'phpadmin.service.wants/'. |
| 150 |
|
| 151 |
The list of Requires may be extended by symlinks in directory |
| 152 |
for the service file. So if there is a file 'phpadmin.service' |
| 153 |
then there MAY be a directory 'phpadmin.service.requires/'. |
| 154 |
|
| 155 |
No other types of directory wants/requires are defined. |
| 156 |
|
| 157 |
### After / Before |
| 158 |
|
| 159 |
Each service may have a list of dependencies. The order of |
| 160 |
their start is determined by the After and Before clauses |
| 161 |
within their files. |
| 162 |
|
| 163 |
Theoretically this should be transitive. So if a dependency |
| 164 |
does have other dependencies then their order should be |
| 165 |
looked up as well. |
| 166 |
|
| 167 |
### list-dependencies |
| 168 |
|
| 169 |
There is a "systemctl list-dependencies" |
| 170 |
|
| 171 |
That one will also show dependencies of dependencies. |
| 172 |
|
| 173 |
Note that we only check the dependencies in the "[Unit]" |
| 174 |
section and not in the "[Install]" section. The install |
| 175 |
section will tell the "enable <unit>" command where to |
| 176 |
set a symlink in a .wants/.requires directory. |
| 177 |
|
| 178 |
As such, after an enable-ment it should pop up as a |
| 179 |
dependency. |