]> git.ipfire.org Git - thirdparty/systemd.git/blob - man/systemd-run.xml
Merge pull request #27621 from esposem/ukify_read
[thirdparty/systemd.git] / man / systemd-run.xml
1 <?xml version='1.0'?>
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-run"
7 xmlns:xi="http://www.w3.org/2001/XInclude">
8
9 <refentryinfo>
10 <title>systemd-run</title>
11 <productname>systemd</productname>
12 </refentryinfo>
13
14 <refmeta>
15 <refentrytitle>systemd-run</refentrytitle>
16 <manvolnum>1</manvolnum>
17 </refmeta>
18
19 <refnamediv>
20 <refname>systemd-run</refname>
21 <refpurpose>Run programs in transient scope units, service units, or path-, socket-, or timer-triggered service units</refpurpose>
22 </refnamediv>
23
24 <refsynopsisdiv>
25 <cmdsynopsis>
26 <command>systemd-run</command>
27 <arg choice="opt" rep="repeat">OPTIONS</arg>
28 <arg choice="plain"><replaceable>COMMAND</replaceable>
29 <arg choice="opt" rep="repeat">ARGS</arg>
30 </arg>
31 </cmdsynopsis>
32 <cmdsynopsis>
33 <command>systemd-run</command>
34 <arg choice="opt" rep="repeat">OPTIONS</arg>
35 <arg choice="opt" rep="repeat">PATH OPTIONS</arg>
36 <arg choice="req"><replaceable>COMMAND</replaceable></arg>
37 <arg choice="opt" rep="repeat">ARGS</arg>
38 </cmdsynopsis>
39 <cmdsynopsis>
40 <command>systemd-run</command>
41 <arg choice="opt" rep="repeat">OPTIONS</arg>
42 <arg choice="opt" rep="repeat">SOCKET OPTIONS</arg>
43 <arg choice="req"><replaceable>COMMAND</replaceable></arg>
44 <arg choice="opt" rep="repeat">ARGS</arg>
45 </cmdsynopsis>
46 <cmdsynopsis>
47 <command>systemd-run</command>
48 <arg choice="opt" rep="repeat">OPTIONS</arg>
49 <arg choice="opt" rep="repeat">TIMER OPTIONS</arg>
50 <arg choice="req"><replaceable>COMMAND</replaceable></arg>
51 <arg choice="opt" rep="repeat">ARGS</arg>
52 </cmdsynopsis>
53 </refsynopsisdiv>
54
55 <refsect1>
56 <title>Description</title>
57
58 <para><command>systemd-run</command> may be used to create and start a transient <filename>.service</filename> or
59 <filename>.scope</filename> unit and run the specified <replaceable>COMMAND</replaceable> in it. It may also be
60 used to create and start a transient <filename>.path</filename>, <filename>.socket</filename>, or
61 <filename>.timer</filename> unit, that activates a <filename>.service</filename> unit when elapsing.</para>
62
63 <para>If a command is run as transient service unit, it will be started and managed by the service manager like any
64 other service, and thus shows up in the output of <command>systemctl list-units</command> like any other unit. It
65 will run in a clean and detached execution environment, with the service manager as its parent process. In this
66 mode, <command>systemd-run</command> will start the service asynchronously in the background and return after the
67 command has begun execution (unless <option>--no-block</option> or <option>--wait</option> are specified, see
68 below).</para>
69
70 <para>If a command is run as transient scope unit, it will be executed by <command>systemd-run</command>
71 itself as parent process and will thus inherit the execution environment of the caller. However, the
72 processes of the command are managed by the service manager similarly to normal services, and will show
73 up in the output of <command>systemctl list-units</command>. Execution in this case is synchronous, and
74 will return only when the command finishes. This mode is enabled via the <option>--scope</option> switch
75 (see below).</para>
76
77 <para>If a command is run with path, socket, or timer options such as <option>--on-calendar=</option> (see below),
78 a transient path, socket, or timer unit is created alongside the service unit for the specified command. Only the
79 transient path, socket, or timer unit is started immediately, the transient service unit will be triggered by the
80 path, socket, or timer unit. If the <option>--unit=</option> option is specified, the
81 <replaceable>COMMAND</replaceable> may be omitted. In this case, <command>systemd-run</command> creates only a
82 <filename>.path</filename>, <filename>.socket</filename>, or <filename>.timer</filename> unit that triggers the
83 specified unit.</para>
84
85 <para>By default, services created with <command>systemd-run</command> default to the
86 <option>simple</option> type, see the description of <varname>Type=</varname> in
87 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry> for
88 details. Note that when this type is used, the service manager (and thus the
89 <command>systemd-run</command> command) considers service start-up successful as soon as the
90 <function>fork()</function> for the main service process succeeded, i.e. before the
91 <function>execve()</function> is invoked, and thus even if the specified command cannot be started.
92 Consider using the <option>exec</option> service type (i.e. <option>--property=Type=exec</option>) to
93 ensure that <command>systemd-run</command> returns successfully only if the specified command line has
94 been successfully started.</para>
95
96 <para>After <command>systemd-run</command> passes the command to the service manager, the manager
97 performs variable expansion. This means that dollar characters (<literal>$</literal>) which should not be
98 expanded need to be escaped as <literal>$$</literal>. Expansion can also be disabled using
99 <varname>--expand-environment=no</varname>.</para>
100 </refsect1>
101
102 <refsect1>
103 <title>Options</title>
104
105 <para>The following options are understood:</para>
106
107 <variablelist>
108 <varlistentry>
109 <term><option>--no-ask-password</option></term>
110
111 <listitem><para>Do not query the user for authentication for
112 privileged operations.</para></listitem>
113 </varlistentry>
114
115 <varlistentry>
116 <term><option>--scope</option></term>
117
118 <listitem>
119 <para>Create a transient <filename>.scope</filename> unit instead of the default transient
120 <filename>.service</filename> unit (see above).
121 </para>
122 </listitem>
123 </varlistentry>
124
125 <varlistentry>
126 <term><option>--unit=</option></term>
127 <term><option>-u</option></term>
128
129 <listitem><para>Use this unit name instead of an automatically
130 generated one.</para></listitem>
131 </varlistentry>
132
133 <varlistentry>
134 <term><option>--property=</option></term>
135 <term><option>-p</option></term>
136
137 <listitem><para>Sets a property on the scope or service unit that is created. This option takes an assignment
138 in the same format as
139 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>'s
140 <command>set-property</command> command.</para>
141 </listitem>
142 </varlistentry>
143
144 <varlistentry>
145 <term><option>--description=</option></term>
146
147 <listitem><para>Provide a description for the service, scope, path, socket, or timer unit. If not specified,
148 the command itself will be used as a description. See <varname>Description=</varname> in
149 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
150 </para></listitem>
151 </varlistentry>
152
153 <varlistentry>
154 <term><option>--slice=</option></term>
155
156 <listitem><para>Make the new <filename>.service</filename> or <filename>.scope</filename> unit part
157 of the specified slice, instead of <filename>system.slice</filename> (when running in
158 <option>--system</option> mode) or the root slice (when running in <option>--user</option>
159 mode).</para>
160 </listitem>
161 </varlistentry>
162
163 <varlistentry>
164 <term><option>--slice-inherit</option></term>
165
166 <listitem><para>Make the new <filename>.service</filename> or <filename>.scope</filename> unit part
167 of the inherited slice. This option can be combined with <option>--slice=</option>.</para>
168
169 <para>An inherited slice is located within <command>systemd-run</command> slice. Example: if
170 <command>systemd-run</command> slice is <filename>foo.slice</filename>, and the
171 <option>--slice=</option> argument is <filename>bar</filename>, the unit will be placed under the
172 <filename>foo-bar.slice</filename>.</para>
173
174 </listitem>
175 </varlistentry>
176
177 <varlistentry>
178 <term><option>--expand-environment=<replaceable>BOOL</replaceable></option></term>
179
180 <listitem><para>Expand environment variables in command arguments. If enabled, environment variables
181 specified as <literal>${<replaceable>VARIABLE</replaceable>}</literal> will be expanded in the same
182 way as in commands specified via <varname>ExecStart=</varname> in units. With
183 <varname>--scope</varname>, this expansion is performed by <command>systemd-run</command> itself, and
184 in other cases by the service manager that spawns the command. Note that this is similar to, but not
185 the same as variable expansion in
186 <citerefentry project='man-pages'><refentrytitle>bash</refentrytitle><manvolnum>1</manvolnum></citerefentry>
187 and other shells.</para>
188
189 <para>The default is to enable this option in all cases, except for <varname>--scope</varname> where
190 it is disabled by default, for backward compatibility reasons. Note that this will be changed in a
191 future release, where it will be switched to enabled by default as well.</para>
192
193 <para>See
194 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
195 for a description of variable expansion. Disabling variable expansion is useful if the specified
196 command includes or may include a <literal>$</literal> sign.</para>
197 </listitem>
198 </varlistentry>
199
200 <varlistentry>
201 <term><option>-r</option></term>
202 <term><option>--remain-after-exit</option></term>
203
204 <listitem><para>After the service process has terminated, keep the service around until it is explicitly
205 stopped. This is useful to collect runtime information about the service after it finished running. Also see
206 <varname>RemainAfterExit=</varname> in
207 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
208 </para>
209 </listitem>
210 </varlistentry>
211
212 <varlistentry>
213 <term><option>--send-sighup</option></term>
214
215 <listitem><para>When terminating the scope or service unit, send a SIGHUP immediately after SIGTERM. This is
216 useful to indicate to shells and shell-like processes that the connection has been severed. Also see
217 <varname>SendSIGHUP=</varname> in
218 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
219 </para>
220 </listitem>
221 </varlistentry>
222
223 <varlistentry>
224 <term><option>--service-type=</option></term>
225
226 <listitem><para>Sets the service type. Also see
227 <varname>Type=</varname> in
228 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>. This
229 option has no effect in conjunction with
230 <option>--scope</option>. Defaults to
231 <constant>simple</constant>.</para>
232 </listitem>
233 </varlistentry>
234
235 <varlistentry>
236 <term><option>--uid=</option></term>
237 <term><option>--gid=</option></term>
238
239 <listitem><para>Runs the service process under the specified UNIX user and group. Also see
240 <varname>User=</varname> and <varname>Group=</varname> in
241 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
242 </listitem>
243 </varlistentry>
244
245 <varlistentry>
246 <term><option>--nice=</option></term>
247
248 <listitem><para>Runs the service process with the specified
249 nice level. Also see <varname>Nice=</varname> in
250 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
251 </listitem>
252 </varlistentry>
253
254 <varlistentry>
255 <term><option>--working-directory=</option></term>
256
257 <listitem><para>Runs the service process with the specified working directory. Also see
258 <varname>WorkingDirectory=</varname> in
259 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
260 </listitem>
261 </varlistentry>
262
263 <varlistentry>
264 <term><option>--same-dir</option></term>
265 <term><option>-d</option></term>
266
267 <listitem><para>Similar to <option>--working-directory=</option>, but uses the current working
268 directory of the caller for the service to execute.</para></listitem>
269 </varlistentry>
270
271 <varlistentry>
272 <term><option>-E <replaceable>NAME</replaceable>[=<replaceable>VALUE</replaceable>]</option></term>
273 <term><option>--setenv=<replaceable>NAME</replaceable>[=<replaceable>VALUE</replaceable>]</option></term>
274
275 <listitem><para>Runs the service process with the specified environment variable set. This parameter
276 may be used more than once to set multiple variables. When <literal>=</literal> and
277 <replaceable>VALUE</replaceable> are omitted, the value of the variable with the same name in the
278 program environment will be used.</para>
279
280 <para>Also see <varname>Environment=</varname> in
281 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
282 </listitem>
283 </varlistentry>
284
285 <varlistentry>
286 <term><option>--pty</option></term>
287 <term><option>-t</option></term>
288
289 <listitem><para>When invoking the command, the transient service connects its standard input, output and error
290 to the terminal <command>systemd-run</command> is invoked on, via a pseudo TTY device. This allows running
291 programs that expect interactive user input/output as services, such as interactive command shells.</para>
292
293 <para>Note that
294 <citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>'s
295 <command>shell</command> command is usually a better alternative for requesting a new, interactive login
296 session on the local host or a local container.</para>
297
298 <para>See below for details on how this switch combines with <option>--pipe</option>.</para></listitem>
299 </varlistentry>
300
301 <varlistentry>
302 <term><option>--pipe</option></term>
303 <term><option>-P</option></term>
304
305 <listitem><para>If specified, standard input, output, and error of the transient service are inherited from the
306 <command>systemd-run</command> command itself. This allows <command>systemd-run</command>
307 to be used within shell pipelines.
308 Note that this mode is not suitable for interactive command shells and similar, as the
309 service process will not become a TTY controller when invoked on a terminal. Use <option>--pty</option> instead
310 in that case.</para>
311
312 <para>When both <option>--pipe</option> and <option>--pty</option> are used in combination the more appropriate
313 option is automatically determined and used. Specifically, when invoked with standard input, output and error
314 connected to a TTY <option>--pty</option> is used, and otherwise <option>--pipe</option>.</para>
315
316 <para>When this option is used the original file descriptors <command>systemd-run</command> receives are passed
317 to the service processes as-is. If the service runs with different privileges than
318 <command>systemd-run</command>, this means the service might not be able to re-open the passed file
319 descriptors, due to normal file descriptor access restrictions. If the invoked process is a shell script that
320 uses the <command>echo "hello" >/dev/stderr</command> construct for writing messages to stderr, this might
321 cause problems, as this only works if stderr can be re-opened. To mitigate this use the construct <command>echo
322 "hello" >&amp;2</command> instead, which is mostly equivalent and avoids this pitfall.</para></listitem>
323 </varlistentry>
324
325 <varlistentry>
326 <term><option>--shell</option></term>
327 <term><option>-S</option></term>
328
329 <listitem><para>A shortcut for <literal>--pty --same-dir --wait --collect --service-type=exec $SHELL</literal>,
330 i.e. requests an interactive shell in the current working directory, running in service context, accessible
331 with a single switch.</para></listitem>
332 </varlistentry>
333
334 <varlistentry>
335 <term><option>--quiet</option></term>
336 <term><option>-q</option></term>
337
338 <listitem><para>Suppresses additional informational output
339 while running. This is particularly useful in combination with
340 <option>--pty</option> when it will suppress the initial
341 message explaining how to terminate the TTY connection.</para></listitem>
342 </varlistentry>
343
344 <varlistentry>
345 <term><option>--on-active=</option></term>
346 <term><option>--on-boot=</option></term>
347 <term><option>--on-startup=</option></term>
348 <term><option>--on-unit-active=</option></term>
349 <term><option>--on-unit-inactive=</option></term>
350
351 <listitem><para>Defines a monotonic timer relative to different starting points for starting the specified
352 command. See <varname>OnActiveSec=</varname>, <varname>OnBootSec=</varname>, <varname>OnStartupSec=</varname>,
353 <varname>OnUnitActiveSec=</varname> and <varname>OnUnitInactiveSec=</varname> in
354 <citerefentry><refentrytitle>systemd.timer</refentrytitle><manvolnum>5</manvolnum></citerefentry> for
355 details. These options are shortcuts for <command>--timer-property=</command> with the relevant properties.
356 These options may not be combined with <option>--scope</option> or <option>--pty</option>.</para>
357 </listitem>
358 </varlistentry>
359
360 <varlistentry>
361 <term><option>--on-calendar=</option></term>
362
363 <listitem><para>Defines a calendar timer for starting the specified command. See <varname>OnCalendar=</varname>
364 in <citerefentry><refentrytitle>systemd.timer</refentrytitle><manvolnum>5</manvolnum></citerefentry>. This
365 option is a shortcut for <command>--timer-property=OnCalendar=</command>. This option may not be combined with
366 <option>--scope</option> or <option>--pty</option>.</para>
367 </listitem>
368 </varlistentry>
369
370 <varlistentry>
371 <term><option>--on-clock-change</option></term>
372 <term><option>--on-timezone-change</option></term>
373
374 <listitem><para>Defines a trigger based on system clock jumps or timezone changes for starting the
375 specified command. See <varname>OnClockChange=</varname> and <varname>OnTimezoneChange=</varname> in
376 <citerefentry><refentrytitle>systemd.timer</refentrytitle><manvolnum>5</manvolnum></citerefentry>. These
377 options are shortcuts for <command>--timer-property=OnClockChange=yes</command> and
378 <command>--timer-property=OnTimezoneChange=yes</command>. These options may not be combined with
379 <option>--scope</option> or <option>--pty</option>.</para></listitem>
380 </varlistentry>
381
382 <varlistentry>
383 <term><option>--path-property=</option></term>
384 <term><option>--socket-property=</option></term>
385 <term><option>--timer-property=</option></term>
386
387 <listitem><para>Sets a property on the path, socket, or timer unit that is created. This option is
388 similar to <option>--property=</option>, but applies to the transient path, socket, or timer unit
389 rather than the transient service unit created. This option takes an assignment in the same format as
390 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>'s
391 <command>set-property</command> command. These options may not be combined with
392 <option>--scope</option> or <option>--pty</option>.</para>
393 </listitem>
394 </varlistentry>
395
396 <varlistentry>
397 <term><option>--no-block</option></term>
398
399 <listitem>
400 <para>Do not synchronously wait for the unit start operation to finish. If this option is not specified, the
401 start request for the transient unit will be verified, enqueued and <command>systemd-run</command> will wait
402 until the unit's start-up is completed. By passing this argument, it is only verified and enqueued. This
403 option may not be combined with <option>--wait</option>.</para>
404 </listitem>
405 </varlistentry>
406
407 <varlistentry>
408 <term><option>--wait</option></term>
409
410 <listitem><para>Synchronously wait for the transient service to terminate. If this option is specified, the
411 start request for the transient unit is verified, enqueued, and waited for. Subsequently the invoked unit is
412 monitored, and it is waited until it is deactivated again (most likely because the specified command
413 completed). On exit, terse information about the unit's runtime is shown, including total runtime (as well as
414 CPU usage, if <option>--property=CPUAccounting=1</option> was set) and the exit code and status of the main
415 process. This output may be suppressed with <option>--quiet</option>. This option may not be combined with
416 <option>--no-block</option>, <option>--scope</option> or the various path, socket, or timer options.</para></listitem>
417 </varlistentry>
418
419 <varlistentry>
420 <term><option>-G</option></term>
421 <term><option>--collect</option></term>
422
423 <listitem><para>Unload the transient unit after it completed, even if it failed. Normally, without this option,
424 all units that ran and failed are kept in memory until the user explicitly resets their failure state with
425 <command>systemctl reset-failed</command> or an equivalent command. On the other hand, units that ran
426 successfully are unloaded immediately. If this option is turned on the "garbage collection" of units is more
427 aggressive, and unloads units regardless if they exited successfully or failed. This option is a shortcut for
428 <command>--property=CollectMode=inactive-or-failed</command>, see the explanation for
429 <varname>CollectMode=</varname> in
430 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry> for further
431 information.</para></listitem>
432 </varlistentry>
433
434 <xi:include href="user-system-options.xml" xpointer="user" />
435 <xi:include href="user-system-options.xml" xpointer="system" />
436 <xi:include href="user-system-options.xml" xpointer="host" />
437 <xi:include href="user-system-options.xml" xpointer="machine" />
438
439 <xi:include href="standard-options.xml" xpointer="help" />
440 <xi:include href="standard-options.xml" xpointer="version" />
441 </variablelist>
442
443 <para>All command line arguments after the first non-option argument become part of the command line of
444 the launched process.</para>
445 </refsect1>
446
447 <refsect1>
448 <title>Exit status</title>
449
450 <para>On success, 0 is returned. If <command>systemd-run</command> failed to start the service, a
451 non-zero return value will be returned. If <command>systemd-run</command> waits for the service to
452 terminate, the return value will be propagated from the service. 0 will be returned on success, including
453 all the cases where systemd considers a service to have exited cleanly, see the discussion of
454 <varname>SuccessExitStatus=</varname> in
455 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
456 </para>
457 </refsect1>
458
459 <refsect1>
460 <title>Examples</title>
461
462 <example>
463 <title>Logging environment variables provided by systemd to services</title>
464
465 <programlisting># systemd-run env
466 Running as unit: run-19945.service
467 # journalctl -u run-19945.service
468 Sep 08 07:37:21 bupkis systemd[1]: Starting /usr/bin/env...
469 Sep 08 07:37:21 bupkis systemd[1]: Started /usr/bin/env.
470 Sep 08 07:37:21 bupkis env[19948]: PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
471 Sep 08 07:37:21 bupkis env[19948]: LANG=en_US.UTF-8
472 Sep 08 07:37:21 bupkis env[19948]: BOOT_IMAGE=/vmlinuz-3.11.0-0.rc5.git6.2.fc20.x86_64</programlisting>
473 </example>
474
475 <example>
476 <title>Limiting resources available to a command</title>
477
478 <programlisting># systemd-run -p IOWeight=10 updatedb</programlisting>
479
480 <para>This command invokes the <citerefentry
481 project='man-pages'><refentrytitle>updatedb</refentrytitle><manvolnum>8</manvolnum></citerefentry>
482 tool, but lowers the block I/O weight for it to 10. See
483 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>
484 for more information on the <varname>IOWeight=</varname> property.</para>
485 </example>
486
487 <example>
488 <title>Running commands at a specified time</title>
489
490 <para>The following command will touch a file after 30 seconds.</para>
491
492 <programlisting># date; systemd-run --on-active=30 --timer-property=AccuracySec=100ms /bin/touch /tmp/foo
493 Mon Dec 8 20:44:24 KST 2014
494 Running as unit: run-71.timer
495 Will run service as unit: run-71.service
496 # journalctl -b -u run-71.timer
497 -- Journal begins at Fri 2014-12-05 19:09:21 KST, ends at Mon 2014-12-08 20:44:54 KST. --
498 Dec 08 20:44:38 container systemd[1]: Starting /bin/touch /tmp/foo.
499 Dec 08 20:44:38 container systemd[1]: Started /bin/touch /tmp/foo.
500 # journalctl -b -u run-71.service
501 -- Journal begins at Fri 2014-12-05 19:09:21 KST, ends at Mon 2014-12-08 20:44:54 KST. --
502 Dec 08 20:44:48 container systemd[1]: Starting /bin/touch /tmp/foo...
503 Dec 08 20:44:48 container systemd[1]: Started /bin/touch /tmp/foo.</programlisting>
504 </example>
505
506 <example>
507 <title>Allowing access to the tty</title>
508
509 <para>The following command invokes <citerefentry project='man-pages'><refentrytitle>bash</refentrytitle><manvolnum>1</manvolnum></citerefentry>
510 as a service passing its standard input, output and error to the calling TTY.</para>
511
512 <programlisting># systemd-run -t --send-sighup bash</programlisting>
513 </example>
514
515 <example>
516 <title>Start <command>screen</command> as a user service</title>
517
518 <programlisting>$ systemd-run --scope --user screen
519 Running scope as unit run-r14b0047ab6df45bfb45e7786cc839e76.scope.
520
521 $ screen -ls
522 There is a screen on:
523 492..laptop (Detached)
524 1 Socket in /var/run/screen/S-fatima.
525 </programlisting>
526
527 <para>This starts the <command>screen</command> process as a child of the
528 <command>systemd --user</command> process that was started by
529 <filename>user@.service</filename>, in a scope unit. A
530 <citerefentry><refentrytitle>systemd.scope</refentrytitle><manvolnum>5</manvolnum></citerefentry>
531 unit is used instead of a
532 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
533 unit, because <command>screen</command> will exit when detaching from the terminal,
534 and a service unit would be terminated. Running <command>screen</command>
535 as a user unit has the advantage that it is not part of the session scope.
536 If <varname>KillUserProcesses=yes</varname> is configured in
537 <citerefentry><refentrytitle>logind.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
538 the default, the session scope will be terminated when the user logs
539 out of that session.</para>
540
541 <para>The <filename>user@.service</filename> is started automatically
542 when the user first logs in, and stays around as long as at least one
543 login session is open. After the user logs out of the last session,
544 <filename>user@.service</filename> and all services underneath it
545 are terminated. This behavior is the default, when "lingering" is
546 not enabled for that user. Enabling lingering means that
547 <filename>user@.service</filename> is started automatically during
548 boot, even if the user is not logged in, and that the service is
549 not terminated when the user logs out.</para>
550
551 <para>Enabling lingering allows the user to run processes without being logged in,
552 for example to allow <command>screen</command> to persist after the user logs out,
553 even if the session scope is terminated. In the default configuration, users can
554 enable lingering for themselves:</para>
555
556 <programlisting>$ loginctl enable-linger</programlisting>
557 </example>
558
559 <example>
560 <title>Variable expansion by the manager</title>
561
562 <programlisting>$ systemd-run -t echo "&lt;${INVOCATION_ID}>" '&lt;${INVOCATION_ID}>'
563 &lt;> &lt;5d0149bfa2c34b79bccb13074001eb20>
564 </programlisting>
565
566 <para>The first argument is expanded by the shell (double quotes), but the second one is not expanded
567 by the shell (single quotes). <command>echo</command> is called with [<literal>/usr/bin/echo</literal>,
568 <literal>[]</literal>, <literal>[${INVOCATION_ID}]</literal>] as the argument array, and then
569 <command>systemd</command> generates <varname>${INVOCATION_ID}</varname> and substitutes it in the
570 command-line. This substitution could not be done on the client side, because the target ID that will
571 be set for the service isn't known before the call is made.</para>
572 </example>
573
574 <example>
575 <title>Variable expansion and output redirection using a shell</title>
576
577 <para>Variable expansion by <command>systemd</command> can be disabled with
578 <varname>--expand-environment=no</varname>.</para>
579
580 <para>Disabling variable expansion can be useful if the command to execute contains dollar characters
581 and escaping them would be inconvenient. For example, when a shell is used:</para>
582
583 <programlisting>$ systemd-run --expand-environment=no -t bash \
584 -c 'echo $SHELL $$ >/dev/stdout'
585 /bin/bash 12345
586 </programlisting>
587
588 <para>The last argument is passed verbatim to the <command>bash</command> shell which is started by the
589 service unit. The shell expands <literal>$SHELL</literal> to the path of the shell, and
590 <literal>$$</literal> to its process number, and then those strings are passed to the
591 <command>echo</command> built-in and printed to standard output (which in this case is connected to the
592 calling terminal).</para>
593 </example>
594
595 <example>
596 <title>Return value</title>
597
598 <programlisting>$ systemd-run --user --wait true
599 $ systemd-run --user --wait -p SuccessExitStatus=11 bash -c 'exit 11'
600 $ systemd-run --user --wait -p SuccessExitStatus=SIGUSR1 --expand-environment=no \
601 bash -c 'kill -SIGUSR1 $$'</programlisting>
602
603 <para>Those three invocations will succeed, i.e. terminate with an exit code of 0.</para>
604 </example>
605 </refsect1>
606
607 <refsect1>
608 <title>See Also</title>
609 <para>
610 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
611 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
612 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
613 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
614 <citerefentry><refentrytitle>systemd.scope</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
615 <citerefentry><refentrytitle>systemd.slice</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
616 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
617 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
618 <citerefentry><refentrytitle>systemd.timer</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
619 <citerefentry><refentrytitle>systemd-mount</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
620 <citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
621 </para>
622 </refsect1>
623
624 </refentry>