Debian Codegraph @

man/systemd-notify.xml

raw
1 <?xml version='1.0'?> <!--*-nxml-*-->
2 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
4 <!-- SPDX-License-Identifier: LGPL-2.1-or-later -->
5
6 <refentry id="systemd-notify"
7 xmlns:xi="http://www.w3.org/2001/XInclude">
8
9 <refentryinfo>
10 <title>systemd-notify</title>
11 <productname>systemd</productname>
12 </refentryinfo>
13
14 <refmeta>
15 <refentrytitle>systemd-notify</refentrytitle>
16 <manvolnum>1</manvolnum>
17 </refmeta>
18
19 <refnamediv>
20 <refname>systemd-notify</refname>
21 <refpurpose>Notify service manager about start-up completion and other daemon status changes</refpurpose>
22 </refnamediv>
23
24 <refsynopsisdiv>
25 <cmdsynopsis>
26 <command>systemd-notify</command> <arg choice="opt" rep="repeat">OPTIONS</arg> <arg choice="opt" rep="repeat">VARIABLE=VALUE</arg>
27 </cmdsynopsis>
28 <cmdsynopsis>
29 <command>systemd-notify</command> <arg choice="plain">--exec</arg> <arg choice="opt" rep="repeat">OPTIONS</arg> <arg choice="opt" rep="repeat">VARIABLE=VALUE</arg> <arg choice="plain">; --</arg> <arg choice="req" rep="repeat">CMDLINE</arg>
30 </cmdsynopsis>
31 <cmdsynopsis>
32 <command>systemd-notify</command> <arg choice="plain">--fork</arg> <arg choice="opt" rep="repeat">OPTIONS</arg> <arg choice="plain">--</arg> <arg choice="req" rep="repeat">CMDLINE</arg>
33 </cmdsynopsis>
34 </refsynopsisdiv>
35
36 <refsect1>
37 <title>Description</title>
38
39 <para><command>systemd-notify</command> may be called by service scripts to notify the invoking service
40 manager about status changes. It can be used to send arbitrary information, encoded in an
41 environment-block-like list of strings. Most importantly, it can be used for start-up completion
42 notification.</para>
43
44 <para>This is mostly just a wrapper around <function>sd_notify()</function> and makes this functionality
45 available to shell scripts. For details see
46 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
47 </para>
48
49 <para>The command line may carry a list of environment variables to send as part of the status
50 update.</para>
51
52 <para>Note that systemd will refuse reception of status updates from this command unless
53 <varname>NotifyAccess=</varname> is appropriately set for the service unit this command is called
54 from. See
55 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry> for
56 details.</para>
57
58 <para>Note that <function>sd_notify()</function> notifications may be attributed to units correctly only
59 if either the sending process is still around at the time the service manager processes the message, or
60 if the sending process is explicitly runtime-tracked by the service manager. The latter is the case if
61 the service manager originally forked off the process, i.e. on all processes that match
62 <varname>NotifyAccess=</varname><option>main</option> or
63 <varname>NotifyAccess=</varname><option>exec</option>. Conversely, if an auxiliary process of the unit
64 sends an <function>sd_notify()</function> message and immediately exits, the service manager might not be
65 able to properly attribute the message to the unit, and thus will ignore it, even if
66 <varname>NotifyAccess=</varname><option>all</option> is set for it. To address this
67 <command>systemd-notify</command> will wait until the notification message has been processed by the
68 service manager. When <option>--no-block</option> is used, this synchronization for reception of
69 notifications is disabled, and hence the aforementioned race may occur if the invoking process is not the
70 service manager or spawned by the service manager.</para>
71
72 <para><command>systemd-notify</command> will first attempt to invoke <function>sd_notify()</function>
73 pretending to have the PID of the parent process of <command>systemd-notify</command> (i.e. the invoking
74 process). This will only succeed when invoked with sufficient privileges. On failure, it will then fall
75 back to invoking it under its own PID. This behaviour is useful in order that when the tool is invoked
76 from a shell script the shell process — and not the <command>systemd-notify</command> process — appears
77 as sender of the message, which in turn is helpful if the shell process is the main process of a service,
78 due to the limitations of <varname>NotifyAccess=</varname><option>all</option>. Use the
79 <option>--pid=</option> switch to tweak this behaviour.</para>
80 </refsect1>
81
82 <refsect1>
83 <title>Options</title>
84
85 <para>The following options are understood:</para>
86
87 <variablelist>
88 <varlistentry>
89 <term><option>--ready</option></term>
90
91 <listitem><para>Inform the invoking service manager about service start-up or configuration reload
92 completion. This is equivalent to <command>systemd-notify READY=1</command>. For details about the
93 semantics of this option see
94 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para></listitem>
95 </varlistentry>
96
97 <varlistentry>
98 <term><option>--reloading</option></term>
99
100 <listitem><para>Inform the invoking service manager about the beginning of a configuration reload
101 cycle. This is equivalent to <command>systemd-notify RELOADING=1</command> (but implicitly also sets
102 a <varname>MONOTONIC_USEC=</varname> field as required for <varname>Type=notify-reload</varname>
103 services, see
104 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
105 for details). For details about the semantics of this option see
106 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
107
108 <xi:include href="version-info.xml" xpointer="v253"/></listitem>
109 </varlistentry>
110
111 <varlistentry>
112 <term><option>--stopping</option></term>
113
114 <listitem><para>Inform the invoking service manager about the beginning of the shutdown phase of the
115 service. This is equivalent to <command>systemd-notify STOPPING=1</command>. For details about the
116 semantics of this option see
117 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
118
119 <xi:include href="version-info.xml" xpointer="v253"/></listitem>
120 </varlistentry>
121
122 <varlistentry>
123 <term><option>--pid=</option></term>
124
125 <listitem><para>Inform the service manager about the main PID of the service. Takes a PID as argument.
126 If the argument is specified as <literal>auto</literal> or omitted, the PID of the process that
127 invoked <command>systemd-notify</command> is used, except if that's the service manager. If the
128 argument is specified as <literal>self</literal>, the PID of the <command>systemd-notify</command>
129 command itself is used, and if <literal>parent</literal> is specified the calling process' PID is
130 used — even if it is the service manager. <option>--pid=auto</option> is equivalent to <command>systemd-notify
131 --pid=$PID</command>. For details about the semantics of this option see
132 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
133
134 <para><command>systemd-notify</command> will first attempt to invoke <function>sd_notify()</function>
135 pretending to have the PID specified with <option>--pid=</option>. This will only succeed when
136 invoked with sufficient privileges. On failure, it will then fall back to invoking it under its own
137 PID. Effectively, this means that a privileged invocation of <command>systemd-notify --pid=</command>
138 may circumvent <varname>NotifyAccess=main</varname> or <varname>NotifyAccess=exec</varname>
139 restrictions enforced for a service.</para>
140
141 <para>If this switch is used in an unprivileged <command>systemd-notify</command> invocation from a
142 process that shall become the new main process of a service — and which is not the process forked off
143 by the service manager (or the current main process) —, then it is essential to set
144 <varname>NotifyAccess=all</varname> in the service unit file, or otherwise the notification will be
145 ignored for security reasons. See
146 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
147 for details.</para></listitem>
148 </varlistentry>
149
150 <varlistentry>
151 <term><option>--uid=<replaceable>USER</replaceable></option></term>
152
153 <listitem><para>Set the user ID to send the notification from. Takes a UNIX user name or numeric UID. When
154 specified the notification message will be sent with the specified UID as sender, in place of the user the
155 command was invoked as. This option requires sufficient privileges in order to be able manipulate the user
156 identity of the process.</para>
157
158 <xi:include href="version-info.xml" xpointer="v237"/></listitem>
159 </varlistentry>
160
161 <varlistentry>
162 <term><option>--status=</option></term>
163
164 <listitem><para>Send a free-form human-readable status string for the daemon to the service
165 manager. This option takes the status string as argument. This is equivalent to
166 <command>systemd-notify STATUS=…</command>. For details about the semantics of this option see
167 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>. This
168 information is shown in
169 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>'s
170 <command>status</command> output, among other places.</para></listitem>
171 </varlistentry>
172
173 <varlistentry>
174 <term><option>--booted</option></term>
175
176 <listitem><para>Returns 0 if the system was booted up with systemd, non-zero otherwise. If this
177 option is passed, no message is sent. This option is hence unrelated to the other options. For
178 details about the semantics of this option, see
179 <citerefentry><refentrytitle>sd_booted</refentrytitle><manvolnum>3</manvolnum></citerefentry>. An
180 alternate way to check for this state is to call
181 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry> with
182 the <command>is-system-running</command> command. It will output <literal>offline</literal> if the
183 system was not booted with systemd, though the return value has a different meaning.</para></listitem>
184 </varlistentry>
185
186 <varlistentry>
187 <term><option>--no-block</option></term>
188
189 <listitem><para>Do not synchronously wait for the requested operation to finish. Use of this option
190 is only recommended when <command>systemd-notify</command> is spawned by the service manager, or when
191 the invoking process is directly spawned by the service manager and has enough privileges to allow
192 <command>systemd-notify</command> to send the notification on its behalf. Sending notifications with
193 this option set is prone to race conditions in all other cases.</para>
194
195 <xi:include href="version-info.xml" xpointer="v246"/></listitem>
196 </varlistentry>
197
198 <varlistentry>
199 <term><option>--exec</option></term>
200
201 <listitem><para>If specified <command>systemd-notify</command> will execute another command line
202 after it completed its operation, replacing its own process. If used, the list of assignments to
203 include in the message sent must be followed by a <literal>;</literal> character (as separate
204 argument), followed by the command line to execute. This permits "chaining" of commands, i.e. issuing
205 one operation, followed immediately by another, without changing PIDs.</para>
206
207 <para>Note that many shells interpret <literal>;</literal> as their own separator for command lines,
208 hence when <command>systemd-notify</command> is invoked from a shell the semicolon must usually be
209 escaped as <literal>\;</literal>.</para>
210
211 <xi:include href="version-info.xml" xpointer="v254"/></listitem>
212 </varlistentry>
213
214 <varlistentry>
215 <term><option>--fd=</option></term>
216
217 <listitem><para>Send a file descriptor along with the notification message. This is useful when
218 invoked in services that have the <varname>FileDescriptorStoreMax=</varname> setting enabled, see
219 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
220 for details. The specified file descriptor must be passed to <command>systemd-notify</command> when
221 invoked. This option may be used multiple times to pass multiple file descriptors in a single
222 notification message.</para>
223
224 <para>To use this functionality from a
225 <citerefentry project='man-pages'><refentrytitle>bash</refentrytitle><manvolnum>1</manvolnum></citerefentry>
226 shell, use an expression like the following:</para>
227 <programlisting>systemd-notify --fd=4 --fd=5 4&lt;/some/file 5&lt;/some/other/file</programlisting>
228
229 <xi:include href="version-info.xml" xpointer="v254"/></listitem>
230 </varlistentry>
231
232 <varlistentry>
233 <term><option>--fdname=</option></term>
234
235 <listitem><para>Set a name to assign to the file descriptors passed via <option>--fd=</option> (see
236 above). This controls the <literal>FDNAME=</literal> field. This setting may only be specified once,
237 and applies to all file descriptors passed. Invoke this tool multiple times in case multiple file
238 descriptors with different file descriptor names shall be submitted.</para>
239
240 <xi:include href="version-info.xml" xpointer="v254"/></listitem>
241 </varlistentry>
242
243 <varlistentry>
244 <term><option>--fork</option></term>
245
246 <listitem><para>Instead of sending a notification message, fork off a command line and wait until a
247 <literal>READY=1</literal> message is received from it. In other words: this makes
248 <command>systemd-notify</command> the receiver of notification messages instead of the sender,
249 swapping roles. This is useful to quickly fork off a process that implements the
250 <function>sd_notify()</function> protocol from a shell script. The invoked command line will have
251 standard input and standard output connected to <filename>/dev/null</filename>, but standard error
252 will be inherited from the invoking process. The numeric process ID is written to standard output by
253 <command>systemd-notify</command> (unless <option>--quiet</option> is specified), which may be used
254 to later terminate the forked off process.</para>
255
256 <para>Note that processes forked off like this will likely remain running after
257 <command>systemd-notify</command> already returned, which hence will result in them being reparented
258 to the closest process reaper process, i.e. typically the per-user or system service manager.</para>
259
260 <para>Note that this option should not be used to invoke full services ad-hoc, use
261 <command>systemd-run</command> for that.</para>
262
263 <para>Also note that when invoked with this switch <command>systemd-notify</command> will exit
264 successfully under two distinction conditions:
265
266 <orderedlist>
267 <listitem><para><command>systemd-notify</command> received a <literal>READY=1</literal>
268 notification from the child it just forked off.</para></listitem>
269
270 <listitem><para>The child process exited cleanly (with exit status zero) before sending
271 <literal>READY=1</literal>.</para></listitem>
272 </orderedlist></para>
273
274 <para>Example use:<programlisting># PID=$(systemd-notify --fork -- mycommand)
275 …
276 kill "$PID"
277 unset PID</programlisting></para>
278
279 <xi:include href="version-info.xml" xpointer="v258"/></listitem>
280 </varlistentry>
281
282 <varlistentry>
283 <term><option>--quiet</option></term>
284 <term><option>-q</option></term>
285
286 <listitem><para>Turn off output of the numeric process ID when <option>--fork</option> is used.</para>
287
288 <xi:include href="version-info.xml" xpointer="v258"/></listitem>
289 </varlistentry>
290
291 <xi:include href="standard-options.xml" xpointer="help" />
292 <xi:include href="standard-options.xml" xpointer="version" />
293 </variablelist>
294
295 </refsect1>
296
297 <refsect1>
298 <title>Exit status</title>
299
300 <para>On success, 0 is returned, a non-zero failure code
301 otherwise.</para>
302 </refsect1>
303
304 <refsect1>
305 <title>Example</title>
306
307 <example>
308 <title>Start-up Notification and Status Updates</title>
309
310 <para>A simple shell daemon that sends start-up notifications after having set up its communication
311 channel. During runtime it sends further status updates to the init system:</para>
312
313 <programlisting>#!/bin/sh
314
315 mkfifo /tmp/waldo
316 systemd-notify --ready --status="Waiting for data…"
317
318 while : ; do
319 read -r a &lt; /tmp/waldo
320 systemd-notify --status="Processing $a"
321
322 # Do something with $a …
323
324 systemd-notify --status="Waiting for data…"
325 done</programlisting>
326 </example>
327 </refsect1>
328
329 <refsect1>
330 <title>See Also</title>
331 <para><simplelist type="inline">
332 <member><citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry></member>
333 <member><citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry></member>
334 <member><citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry></member>
335 <member><citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry></member>
336 <member><citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
337 <member><citerefentry><refentrytitle>sd_booted</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
338 </simplelist></para>
339 </refsect1>
340 </refentry>