| 1 |
# Tricks and Hints |
| 2 |
|
| 3 |
## The --now internals |
| 4 |
|
| 5 |
The option "--now" is only used for a very low number |
| 6 |
of systemctl commands. In the systemctl.py variant |
| 7 |
it is (ab)used to run an alternative method. |
| 8 |
|
| 9 |
## systemctl.py list-unit-files --now |
| 10 |
|
| 11 |
Shows the list of files found on the disk. Those are |
| 12 |
not parsed and they are not check for an enabled-link. |
| 13 |
It is just what is there as a first step after scanning |
| 14 |
for systemd-relevant unit information. |
| 15 |
|
| 16 |
## systemctl.py list-dependencies --now |
| 17 |
|
| 18 |
Shows the start-order of services. Use -v debugging to |
| 19 |
see how the start-rank is computed from the Before/After |
| 20 |
clauses in the unit files. |
| 21 |
|
| 22 |
Note that upon a "<stop> operation" the start order is |
| 23 |
run through in reverse order. |
| 24 |
|
| 25 |
The current 'default' target does not filter the list. |
| 26 |
Instead it only uses Before/After on the units in the |
| 27 |
'multi-user.target' but it does not process any of |
| 28 |
its required units. |
| 29 |
|
| 30 |
# Implementation helpers |
| 31 |
|
| 32 |
## docker images --filter |
| 33 |
|
| 34 |
https://github.com/moby/moby/blob/10c0af083544460a2ddc2218f37dc24a077f7d90/docs/reference/commandline/images.md#filtering |
| 35 |
|
| 36 |
docker images --filter dangling=true || dangling=false |
| 37 |
docker images --filter label=<key> || label=<key>=<value> |
| 38 |
|
| 39 |
whereas filter by name is just an argument "docker images <name>" |
| 40 |
|
| 41 |
## docker images --format |
| 42 |
|
| 43 |
docker images --format "{{.ID}}: {{.Repository}}\t{{.Size}}" |
| 44 |
and "{{.Tag}} {{.Digest}} {{.CreatedSince}} {{.CreatedAt}}" |
| 45 |
|
| 46 |
## IPv4 -vs- IPv6 |
| 47 |
|
| 48 |
For java / tomcat applications one can add a java-option to use ipv4 |
| 49 |
|
| 50 |
-Djava.net.preferIPv4Stack=true |
| 51 |
|
| 52 |
For docker-compose 2.1+ one can add a sysctl to disable ipv6. |
| 53 |
|
| 54 |
sysctls: |
| 55 |
- net.ipv6.conf.eth0.disable_ipv6=1 |
| 56 |
- net.ipv6.conf.all.disable_ipv6=1 |
| 57 |
|
| 58 |
And there is an option to disable '::1 localhost' with systemctl.py |
| 59 |
|
| 60 |
systemctl.py --ipv4 daemon-reload |
| 61 |
|
| 62 |
## oneshoot services |
| 63 |
|
| 64 |
https://www.freedesktop.org/software/systemd/man/systemd.service.html |
| 65 |
|
| 66 |
There is a note that after "start unit" the unit is considered to be |
| 67 |
active. A second "start unit" shall NOT run the execs again. |
| 68 |
|
| 69 |
Indirectly it hints to the detail that a "start unit" does not run |
| 70 |
any execs when "is-active unit" true. See issue #6. |
| 71 |
|
| 72 |
## system specifies |
| 73 |
|
| 74 |
https://www.freedesktop.org/software/systemd/man/systemd.unit.html |
| 75 |
|
| 76 |
* "%n" Full unit name |
| 77 |
* "%N" Unescaped full unit name = Same as "%n", but with escaping undone |
| 78 |
* "%p" Prefix name = For instantiated units, this refers to the string before the "@" character of the unit name. For non-instantiated units, this refers to the name of the unit with the type suffix removed. |
| 79 |
* "%P" Unescaped prefix name = Same as "%p", but with escaping undone |
| 80 |
* "%i" Instance name = For instantiated units: this is the string between the "@" character and the suffix of the unit name. |
| 81 |
* "%I" Unescaped instance name = Same as "%i", but with escaping undone |
| 82 |
* "%f" Unescaped filename = This is either the unescaped instance name (if applicable) with / prepended (if applicable), or the unescaped prefix name prepended with /. |
| 83 |
* "%t" Runtime directory = This is either /run (for the system manager) or the path "$XDG_RUNTIME_DIR" resolves to (for user managers). |
| 84 |
* "%u" User name = This is the name of the user running the service manager instance. In case of the system manager this resolves to "root". |
| 85 |
* "%U" User UID = This is the numeric UID of the user running the service manager instance. In case of the system manager this resolves to "0". |
| 86 |
* "%h" User home directory = This is the home directory of the user running the service manager instance. In case of the system manager this resolves to "/root". |
| 87 |
* "%s" User shell = This is the shell of the user running the service manager instance. In case of the system manager this resolves to "/bin/sh". |
| 88 |
* "%m" Machine ID = The machine ID of the running system, formatted as string. See machine-id(5) for more information. |
| 89 |
* "%b" Boot ID = The boot ID of the running system, formatted as string. See random(4) for more information. |
| 90 |
* "%H" Host name = The hostname of the running system at the point in time the unit configuration is loaded. |
| 91 |
* "%v" Kernel release = Identical to uname -r output |
| 92 |
* "%%" Single percent sign = Use "%%" in place of "%" to specify a single percent sign. |
| 93 |
|
| 94 |
## KillMode with SendSIGKILL and SendSIGHUP |
| 95 |
|
| 96 |
When grepping through existing *.service files one can see a number |
| 97 |
of references to those two options |
| 98 |
* SendSIGKILL=no / postgresql + plymouth-start |
| 99 |
* SendSIGHUP=yes / getty + console + journal-service |
| 100 |
|
| 101 |
For a docker container one can safely exclude the latter but the |
| 102 |
usage for postgresql makes it an importation option being in use. |
| 103 |
In both cases the option changes the behaviour of "systemctl kill". |
| 104 |
|
| 105 |
https://www.freedesktop.org/software/systemd/man/systemd.kill.html |
| 106 |
|
| 107 |
As one can already guess, the default for SendSIGHUP is "no" and it |
| 108 |
means that after the "KillSignal=" it should immediately send an |
| 109 |
addtional SIGHUP - which is defined to also be forwarded to the |
| 110 |
children of a program. |
| 111 |
|
| 112 |
One can also guess the default for SendSIGKILL to be "yes". Here it |
| 113 |
is defined to work only after a timeout - as it shall send a KILL |
| 114 |
to all remaining processes(!) of a service that are still around. |
| 115 |
|
| 116 |
The third signal in use is a SIGTERM which is the default that is |
| 117 |
sometimes overridden as "KillSignal=SIGHUP". Obviously it does |
| 118 |
not make sense to mix Kill-SIGHUP and SendSIGHUP and there is no |
| 119 |
such thing to exist in real service systemd files. |
| 120 |
|
| 121 |
Last not least, there is also a "KillMode=control-group" default. |
| 122 |
Which effectivly means that the specified sequences of Signals |
| 123 |
(SIGTERM + SIGHUP & SIGKILL) should be sent to all processes. |
| 124 |
|
| 125 |
As it is implemented for systemctl.py 1.0 there are some differences |
| 126 |
in the handling - a "systemctl.py stop" will only execute ExecStop |
| 127 |
but no implicit "systemctl kill" after that (KillMode=none). And |
| 128 |
the SIGTERM & SIGKILL will only go the $MAINPID of a service as far |
| 129 |
as we know about it (KillMode=process). If there is NO ExecStop |
| 130 |
then there is a fallback from "signal stop" to "signal kill". |
| 131 |
|
| 132 |
## Start Exec-Mode |
| 133 |
|
| 134 |
It may be a little known detail from the systemd specs but in |
| 135 |
reality one should NOT have multiple ExecStart lines in a spec |
| 136 |
file. (quoting from specs: "Unless Type= is oneshot, exactly |
| 137 |
one command must be given.") |
| 138 |
|
| 139 |
Instead the systemd specification has a number of prefixes for |
| 140 |
the ExecStart line that modify the behaviour that are somewhat |
| 141 |
related to the call of fork+execve. |
| 142 |
|
| 143 |
https://www.freedesktop.org/software/systemd/man/systemd.service.html#ExecStart= |
| 144 |
|
| 145 |
* a "-" prefix is the most obvious: ignore the return value |
| 146 |
* a "@" prefix allows to have argv[0] to be not the called program |
| 147 |
* a "+" prefix makes for a priviledged services process which |
| 148 |
basically means that User/Group changes should NOT be made |
| 149 |
(User/Group would affect ExecStart/ExecStop otherwise) |
| 150 |
* and some unintelligible description of "!" and "!!" prefixes |
| 151 |
the seem to be alterations of a "+" |
| 152 |
|
| 153 |
Moreover, "-" and "@" and "+" may be combined and the prefix |
| 154 |
characters may occur in any order. Phew! |
| 155 |
|
| 156 |
And just for another thing - one might expect that along with |
| 157 |
ExecStart / ExecStop / ExecReload there also Exec-statements |
| 158 |
for other frontend commands - including ExecXyPre/ExecXyPost |
| 159 |
statements for each of them. But this is not the case - |
| 160 |
specifically there exists NO ExecRestart statement in the |
| 161 |
systemd specs. Only these are valid: |
| 162 |
|
| 163 |
* ExecStartPre |
| 164 |
* ExecStart |
| 165 |
* ExecStartPost |
| 166 |
* ExecReload |
| 167 |
* ExecStop |
| 168 |
* ExecStopPost |
| 169 |
|
| 170 |
On top of that, if any of the ExecStopPre, ExecStart, ExecStopPost |
| 171 |
commands fails then you will also see that the original systemd |
| 172 |
daemon will execute the ExecStopPost lines to cleanup the system. |
| 173 |
(and it will NOT run the ExecStop lines in that case). In order |
| 174 |
for ExecStopPost to know the reason why it has been called there |
| 175 |
are additional environment variables around |
| 176 |
|
| 177 |
* $SERVICE_RESULT = "success", "timeout", "exit-code", "signal".. |
| 178 |
* $EXIT_CODE = "exited", "killed" (the most relevant styles) |
| 179 |
* $EXIT_STATUS - the exit-code of $MAINPID .. and for the |
| 180 |
combination of "success" + "exited" this is always 0. |
| 181 |
|
| 182 |
That's a bit tricky to follow and "systemctl.py" does not do it. |
| 183 |
One can assume that there is hardly any programmer to have taken |
| 184 |
advantage of that - it sounds a bit like wizardry around. |
| 185 |
|
| 186 |
As for the hint of "only one ExecStart" - while the script after |
| 187 |
ExecStart is NOT run in a shell you can still use multiple |
| 188 |
commands by having them seperated with "{space}{semicolon}{space}". |
| 189 |
And a stray semicolon may instead be escaped with "\;". Phew! |
| 190 |
|
| 191 |
|