]> git.ipfire.org Git - thirdparty/systemd.git/blob - man/systemd-notify.xml
77ce2b5b6d2205e4fccadbd5705814bfc12ce16b
[thirdparty/systemd.git] / man / systemd-notify.xml
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.2/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 <arg choice="opt" rep="repeat">OPTIONS</arg> <arg choice="opt" rep="repeat">VARIABLE=VALUE</arg></command>
27 </cmdsynopsis>
28 <cmdsynopsis>
29 <command>systemd-notify --exec <arg choice="opt" rep="repeat">OPTIONS</arg> <arg choice="opt" rep="repeat">VARIABLE=VALUE</arg> ; <arg rep="repeat">CMDLINE</arg></command>
30 </cmdsynopsis>
31 </refsynopsisdiv>
32
33 <refsect1>
34 <title>Description</title>
35
36 <para><command>systemd-notify</command> may be called by service scripts to notify the invoking service
37 manager about status changes. It can be used to send arbitrary information, encoded in an
38 environment-block-like list of strings. Most importantly, it can be used for start-up completion
39 notification.</para>
40
41 <para>This is mostly just a wrapper around <function>sd_notify()</function> and makes this functionality
42 available to shell scripts. For details see
43 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
44 </para>
45
46 <para>The command line may carry a list of environment variables to send as part of the status
47 update.</para>
48
49 <para>Note that systemd will refuse reception of status updates from this command unless
50 <varname>NotifyAccess=</varname> is appropriately set for the service unit this command is called
51 from. See
52 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry> for
53 details.</para>
54
55 <para>Note that <function>sd_notify()</function> notifications may be attributed to units correctly only
56 if either the sending process is still around at the time the service manager processes the message, or
57 if the sending process is explicitly runtime-tracked by the service manager. The latter is the case if
58 the service manager originally forked off the process, i.e. on all processes that match
59 <varname>NotifyAccess=</varname><option>main</option> or
60 <varname>NotifyAccess=</varname><option>exec</option>. Conversely, if an auxiliary process of the unit
61 sends an <function>sd_notify()</function> message and immediately exits, the service manager might not be
62 able to properly attribute the message to the unit, and thus will ignore it, even if
63 <varname>NotifyAccess=</varname><option>all</option> is set for it. To address this
64 <command>systemd-notify</command> will wait until the notification message has been processed by the
65 service manager. When <option>--no-block</option> is used, this synchronization for reception of
66 notifications is disabled, and hence the aforementioned race may occur if the invoking process is not the
67 service manager or spawned by the service manager.</para>
68
69 <para><command>systemd-notify</command> will first attempt to invoke <function>sd_notify()</function>
70 pretending to have the PID of the parent process of <command>systemd-notify</command> (i.e. the invoking
71 process). This will only succeed when invoked with sufficient privileges. On failure, it will then fall
72 back to invoking it under its own PID. This behaviour is useful in order that when the tool is invoked
73 from a shell script the shell process — and not the <command>systemd-notify</command> process — appears
74 as sender of the message, which in turn is helpful if the shell process is the main process of a service,
75 due to the limitations of <varname>NotifyAccess=</varname><option>all</option>. Use the
76 <option>--pid=</option> switch to tweak this behaviour.</para>
77 </refsect1>
78
79 <refsect1>
80 <title>Options</title>
81
82 <para>The following options are understood:</para>
83
84 <variablelist>
85 <varlistentry>
86 <term><option>--ready</option></term>
87
88 <listitem><para>Inform the invoking service manager about service start-up or configuration reload
89 completion. This is equivalent to <command>systemd-notify READY=1</command>. For details about the
90 semantics of this option see
91 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para></listitem>
92 </varlistentry>
93
94 <varlistentry>
95 <term><option>--reloading</option></term>
96
97 <listitem><para>Inform the invoking service manager about the beginning of a configuration reload
98 cycle. This is equivalent to <command>systemd-notify RELOADING=1</command> (but implicitly also sets
99 a <varname>MONOTONIC_USEC=</varname> field as required for <varname>Type=notify-reload</varname>
100 services, see
101 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
102 for details). For details about the semantics of this option see
103 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
104
105 <xi:include href="version-info.xml" xpointer="v253"/></listitem>
106 </varlistentry>
107
108 <varlistentry>
109 <term><option>--stopping</option></term>
110
111 <listitem><para>Inform the invoking service manager about the beginning of the shutdown phase of the
112 service. This is equivalent to <command>systemd-notify STOPPING=1</command>. For details about the
113 semantics of this option see
114 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
115
116 <xi:include href="version-info.xml" xpointer="v253"/></listitem>
117 </varlistentry>
118
119 <varlistentry>
120 <term><option>--pid=</option></term>
121
122 <listitem><para>Inform the service manager about the main PID of the service. Takes a PID as
123 argument. If the argument is specified as <literal>auto</literal> or omitted, the PID of the process
124 that invoked <command>systemd-notify</command> is used, except if that's the service manager. If the
125 argument is specified as <literal>self</literal>, the PID of the <command>systemd-notify</command>
126 command itself is used, and if <literal>parent</literal> is specified the calling process' PID is
127 used — even if it is the service manager. The latter is equivalent to <command>systemd-notify
128 MAINPID=$PID</command>. For details about the semantics of this option see
129 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
130
131 <para>If this switch is used in an <command>systemd-notify</command> invocation from a process that
132 shall become the new main process of a service — and which is not the process forked off by the
133 service manager (or the current main process) —, then it is essential to set
134 <varname>NotifyAccess=all</varname> in the service unit file, or otherwise the notification will be
135 ignored for security reasons. See
136 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
137 for details.</para></listitem>
138 </varlistentry>
139
140 <varlistentry>
141 <term><option>--uid=</option><replaceable>USER</replaceable></term>
142
143 <listitem><para>Set the user ID to send the notification from. Takes a UNIX user name or numeric UID. When
144 specified the notification message will be sent with the specified UID as sender, in place of the user the
145 command was invoked as. This option requires sufficient privileges in order to be able manipulate the user
146 identity of the process.</para>
147
148 <xi:include href="version-info.xml" xpointer="v237"/></listitem>
149 </varlistentry>
150
151 <varlistentry>
152 <term><option>--status=</option></term>
153
154 <listitem><para>Send a free-form human readable status string for the daemon to the service
155 manager. This option takes the status string as argument. This is equivalent to
156 <command>systemd-notify STATUS=…</command>. For details about the semantics of this option see
157 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>. This
158 information is shown in
159 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>'s
160 <command>status</command> output, among other places.</para></listitem>
161 </varlistentry>
162
163 <varlistentry>
164 <term><option>--booted</option></term>
165
166 <listitem><para>Returns 0 if the system was booted up with systemd, non-zero otherwise. If this
167 option is passed, no message is sent. This option is hence unrelated to the other options. For
168 details about the semantics of this option, see
169 <citerefentry><refentrytitle>sd_booted</refentrytitle><manvolnum>3</manvolnum></citerefentry>. An
170 alternate way to check for this state is to call
171 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry> with
172 the <command>is-system-running</command> command. It will return <literal>offline</literal> if the
173 system was not booted with systemd. </para></listitem>
174 </varlistentry>
175
176 <varlistentry>
177 <term><option>--no-block</option></term>
178
179 <listitem><para>Do not synchronously wait for the requested operation to finish. Use of this option
180 is only recommended when <command>systemd-notify</command> is spawned by the service manager, or when
181 the invoking process is directly spawned by the service manager and has enough privileges to allow
182 <command>systemd-notify</command> to send the notification on its behalf. Sending notifications with
183 this option set is prone to race conditions in all other cases.</para>
184
185 <xi:include href="version-info.xml" xpointer="v246"/></listitem>
186 </varlistentry>
187
188 <varlistentry>
189 <term><option>--exec</option></term>
190
191 <listitem><para>If specified <command>systemd-notify</command> will execute another command line
192 after it completed its operation, replacing its own process. If used, the list of assignments to
193 include in the message sent must be followed by a <literal>;</literal> character (as separate
194 argument), followed by the command line to execute. This permits "chaining" of commands, i.e. issuing
195 one operation, followed immediately by another, without changing PIDs.</para>
196
197 <para>Note that many shells interpret <literal>;</literal> as their own separator for command lines,
198 hence when <command>systemd-notify</command> is invoked from a shell the semicolon must usually be
199 escaped as <literal>\;</literal>.</para>
200
201 <xi:include href="version-info.xml" xpointer="v254"/></listitem>
202 </varlistentry>
203
204 <varlistentry>
205 <term><option>--fd=</option></term>
206
207 <listitem><para>Send a file descriptor along with the notification message. This is useful when
208 invoked in services that have the <varname>FileDescriptorStoreMax=</varname> setting enabled, see
209 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
210 for details. The specified file descriptor must be passed to <command>systemd-notify</command> when
211 invoked. This option may be used multiple times to pass multiple file descriptors in a single
212 notification message.</para>
213
214 <para>To use this functionality from a <command>bash</command> shell, use an expression like the following:</para>
215 <programlisting>systemd-notify --fd=4 --fd=5 4&lt;/some/file 5&lt;/some/other/file</programlisting>
216
217 <xi:include href="version-info.xml" xpointer="v254"/></listitem>
218 </varlistentry>
219
220 <varlistentry>
221 <term><option>--fdname=</option></term>
222
223 <listitem><para>Set a name to assign to the file descriptors passed via <option>--fd=</option> (see
224 above). This controls the <literal>FDNAME=</literal> field. This setting may only be specified once,
225 and applies to all file descriptors passed. Invoke this tool multiple times in case multiple file
226 descriptors with different file descriptor names shall be submitted.</para>
227
228 <xi:include href="version-info.xml" xpointer="v254"/></listitem>
229 </varlistentry>
230
231 <xi:include href="standard-options.xml" xpointer="help" />
232 <xi:include href="standard-options.xml" xpointer="version" />
233 </variablelist>
234
235 </refsect1>
236
237 <refsect1>
238 <title>Exit status</title>
239
240 <para>On success, 0 is returned, a non-zero failure code
241 otherwise.</para>
242 </refsect1>
243
244 <refsect1>
245 <title>Example</title>
246
247 <example>
248 <title>Start-up Notification and Status Updates</title>
249
250 <para>A simple shell daemon that sends start-up notifications after having set up its communication
251 channel. During runtime it sends further status updates to the init system:</para>
252
253 <programlisting>#!/bin/sh
254
255 mkfifo /tmp/waldo
256 systemd-notify --ready --status="Waiting for data…"
257
258 while : ; do
259 read -r a &lt; /tmp/waldo
260 systemd-notify --status="Processing $a"
261
262 # Do something with $a …
263
264 systemd-notify --status="Waiting for data…"
265 done</programlisting>
266 </example>
267 </refsect1>
268
269 <refsect1>
270 <title>See Also</title>
271 <para>
272 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
273 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
274 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
275 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
276 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
277 <citerefentry><refentrytitle>sd_booted</refentrytitle><manvolnum>3</manvolnum></citerefentry>
278 </para>
279 </refsect1>
280 </refentry>