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 -->
6 <refentry id=
"systemd-notify"
7 xmlns:
xi=
"http://www.w3.org/2001/XInclude">
10 <title>systemd-notify
</title>
11 <productname>systemd
</productname>
15 <refentrytitle>systemd-notify
</refentrytitle>
16 <manvolnum>1</manvolnum>
20 <refname>systemd-notify
</refname>
21 <refpurpose>Notify service manager about start-up completion and other daemon status changes
</refpurpose>
26 <command>systemd-notify
</command> <arg choice=
"opt" rep=
"repeat">OPTIONS
</arg> <arg choice=
"opt" rep=
"repeat">VARIABLE=VALUE
</arg>
29 <command>systemd-notify
</command> <arg>--exec
</arg> <arg choice=
"opt" rep=
"repeat">OPTIONS
</arg> <arg choice=
"opt" rep=
"repeat">VARIABLE=VALUE
</arg> <arg>;
</arg> <arg rep=
"repeat">CMDLINE
</arg>
34 <title>Description
</title>
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
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>.
46 <para>The command line may carry a list of environment variables to send as part of the status
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
52 <citerefentry><refentrytitle>systemd.service
</refentrytitle><manvolnum>5</manvolnum></citerefentry> for
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>
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>
80 <title>Options
</title>
82 <para>The following options are understood:
</para>
86 <term><option>--ready
</option></term>
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>
95 <term><option>--reloading
</option></term>
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>
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>
105 <xi:include href=
"version-info.xml" xpointer=
"v253"/></listitem>
109 <term><option>--stopping
</option></term>
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>
116 <xi:include href=
"version-info.xml" xpointer=
"v253"/></listitem>
120 <term><option>--pid=
</option></term>
122 <listitem><para>Inform the service manager about the main PID of the service. Takes a PID as argument.
123 If the argument is specified as
<literal>auto
</literal> or omitted, the PID of the process that
124 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.
<option>--pid=auto
</option> 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>
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>
141 <term><option>--uid=
<replaceable>USER
</replaceable></option></term>
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>
148 <xi:include href=
"version-info.xml" xpointer=
"v237"/></listitem>
152 <term><option>--status=
</option></term>
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>
164 <term><option>--booted
</option></term>
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>
177 <term><option>--no-block
</option></term>
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>
185 <xi:include href=
"version-info.xml" xpointer=
"v246"/></listitem>
189 <term><option>--exec
</option></term>
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>
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>
201 <xi:include href=
"version-info.xml" xpointer=
"v254"/></listitem>
205 <term><option>--fd=
</option></term>
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>
214 <para>To use this functionality from a
215 <citerefentry project='man-pages'
><refentrytitle>bash
</refentrytitle><manvolnum>1</manvolnum></citerefentry>
216 shell, use an expression like the following:
</para>
217 <programlisting>systemd-notify --fd=
4 --fd=
5 4</some/file
5</some/other/file
</programlisting>
219 <xi:include href=
"version-info.xml" xpointer=
"v254"/></listitem>
223 <term><option>--fdname=
</option></term>
225 <listitem><para>Set a name to assign to the file descriptors passed via
<option>--fd=
</option> (see
226 above). This controls the
<literal>FDNAME=
</literal> field. This setting may only be specified once,
227 and applies to all file descriptors passed. Invoke this tool multiple times in case multiple file
228 descriptors with different file descriptor names shall be submitted.
</para>
230 <xi:include href=
"version-info.xml" xpointer=
"v254"/></listitem>
233 <xi:include href=
"standard-options.xml" xpointer=
"help" />
234 <xi:include href=
"standard-options.xml" xpointer=
"version" />
240 <title>Exit status
</title>
242 <para>On success,
0 is returned, a non-zero failure code
247 <title>Example
</title>
250 <title>Start-up Notification and Status Updates
</title>
252 <para>A simple shell daemon that sends start-up notifications after having set up its communication
253 channel. During runtime it sends further status updates to the init system:
</para>
255 <programlisting>#!/bin/sh
258 systemd-notify --ready
--status=
"Waiting for data…"
261 read -r a
< /tmp/waldo
262 systemd-notify
--status=
"Processing $a"
264 # Do something with $a …
266 systemd-notify
--status=
"Waiting for data…"
267 done
</programlisting>
272 <title>See Also
</title>
273 <para><simplelist type=
"inline">
274 <member><citerefentry><refentrytitle>systemd
</refentrytitle><manvolnum>1</manvolnum></citerefentry></member>
275 <member><citerefentry><refentrytitle>systemctl
</refentrytitle><manvolnum>1</manvolnum></citerefentry></member>
276 <member><citerefentry><refentrytitle>systemd.unit
</refentrytitle><manvolnum>5</manvolnum></citerefentry></member>
277 <member><citerefentry><refentrytitle>systemd.service
</refentrytitle><manvolnum>5</manvolnum></citerefentry></member>
278 <member><citerefentry><refentrytitle>sd_notify
</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
279 <member><citerefentry><refentrytitle>sd_booted
</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>