]> git.ipfire.org Git - thirdparty/systemd.git/blob - man/systemd-run.xml
run/uid0: tint the terminal background color (and add new --background= switch)
[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>
113
114 <xi:include href="version-info.xml" xpointer="v226"/></listitem>
115 </varlistentry>
116
117 <varlistentry>
118 <term><option>--scope</option></term>
119
120 <listitem>
121 <para>Create a transient <filename>.scope</filename> unit instead of the default transient
122 <filename>.service</filename> unit (see above).
123 </para>
124
125 <xi:include href="version-info.xml" xpointer="v206"/>
126 </listitem>
127 </varlistentry>
128
129 <varlistentry>
130 <term><option>--unit=</option></term>
131 <term><option>-u</option></term>
132
133 <listitem><para>Use this unit name instead of an automatically
134 generated one.</para>
135
136 <xi:include href="version-info.xml" xpointer="v206"/></listitem>
137 </varlistentry>
138
139 <varlistentry>
140 <term><option>--property=</option></term>
141 <term><option>-p</option></term>
142
143 <listitem><para>Sets a property on the scope or service unit that is created. This option takes an assignment
144 in the same format as
145 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>'s
146 <command>set-property</command> command.</para>
147
148 <xi:include href="version-info.xml" xpointer="v211"/>
149 </listitem>
150 </varlistentry>
151
152 <varlistentry>
153 <term><option>--description=</option></term>
154
155 <listitem><para>Provide a description for the service, scope, path, socket, or timer unit. If not specified,
156 the command itself will be used as a description. See <varname>Description=</varname> in
157 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
158 </para>
159
160 <xi:include href="version-info.xml" xpointer="v206"/></listitem>
161 </varlistentry>
162
163 <varlistentry>
164 <term><option>--slice=</option></term>
165
166 <listitem><para>Make the new <filename>.service</filename> or <filename>.scope</filename> unit part
167 of the specified slice, instead of <filename>system.slice</filename> (when running in
168 <option>--system</option> mode) or the root slice (when running in <option>--user</option>
169 mode).</para>
170
171 <xi:include href="version-info.xml" xpointer="v206"/>
172 </listitem>
173 </varlistentry>
174
175 <varlistentry>
176 <term><option>--slice-inherit</option></term>
177
178 <listitem><para>Make the new <filename>.service</filename> or <filename>.scope</filename> unit part
179 of the inherited slice. This option can be combined with <option>--slice=</option>.</para>
180
181 <para>An inherited slice is located within <command>systemd-run</command> slice. Example: if
182 <command>systemd-run</command> slice is <filename>foo.slice</filename>, and the
183 <option>--slice=</option> argument is <filename>bar</filename>, the unit will be placed under the
184 <filename>foo-bar.slice</filename>.</para>
185
186 <xi:include href="version-info.xml" xpointer="v246"/>
187
188 </listitem>
189 </varlistentry>
190
191 <varlistentry>
192 <term><option>--expand-environment=<replaceable>BOOL</replaceable></option></term>
193
194 <listitem><para>Expand environment variables in command arguments. If enabled, environment variables
195 specified as <literal>${<replaceable>VARIABLE</replaceable>}</literal> will be expanded in the same
196 way as in commands specified via <varname>ExecStart=</varname> in units. With
197 <varname>--scope</varname>, this expansion is performed by <command>systemd-run</command> itself, and
198 in other cases by the service manager that spawns the command. Note that this is similar to, but not
199 the same as variable expansion in
200 <citerefentry project='man-pages'><refentrytitle>bash</refentrytitle><manvolnum>1</manvolnum></citerefentry>
201 and other shells.</para>
202
203 <para>The default is to enable this option in all cases, except for <varname>--scope</varname> where
204 it is disabled by default, for backward compatibility reasons. Note that this will be changed in a
205 future release, where it will be switched to enabled by default as well.</para>
206
207 <para>See
208 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
209 for a description of variable expansion. Disabling variable expansion is useful if the specified
210 command includes or may include a <literal>$</literal> sign.</para>
211
212 <xi:include href="version-info.xml" xpointer="v254"/>
213 </listitem>
214 </varlistentry>
215
216 <varlistentry>
217 <term><option>-r</option></term>
218 <term><option>--remain-after-exit</option></term>
219
220 <listitem><para>After the service process has terminated, keep the service around until it is explicitly
221 stopped. This is useful to collect runtime information about the service after it finished running. Also see
222 <varname>RemainAfterExit=</varname> in
223 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
224 </para>
225
226 <xi:include href="version-info.xml" xpointer="v207"/>
227 </listitem>
228 </varlistentry>
229
230 <varlistentry>
231 <term><option>--send-sighup</option></term>
232
233 <listitem><para>When terminating the scope or service unit, send a SIGHUP immediately after SIGTERM. This is
234 useful to indicate to shells and shell-like processes that the connection has been severed. Also see
235 <varname>SendSIGHUP=</varname> in
236 <citerefentry><refentrytitle>systemd.kill</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
237 </para>
238
239 <xi:include href="version-info.xml" xpointer="v207"/>
240 </listitem>
241 </varlistentry>
242
243 <varlistentry>
244 <term><option>--service-type=</option></term>
245
246 <listitem><para>Sets the service type. Also see
247 <varname>Type=</varname> in
248 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>. This
249 option has no effect in conjunction with
250 <option>--scope</option>. Defaults to
251 <constant>simple</constant>.</para>
252
253 <xi:include href="version-info.xml" xpointer="v211"/>
254 </listitem>
255 </varlistentry>
256
257 <varlistentry>
258 <term><option>--uid=</option></term>
259 <term><option>--gid=</option></term>
260
261 <listitem><para>Runs the service process under the specified UNIX user and group. Also see
262 <varname>User=</varname> and <varname>Group=</varname> in
263 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
264
265 <xi:include href="version-info.xml" xpointer="v211"/>
266 </listitem>
267 </varlistentry>
268
269 <varlistentry>
270 <term><option>--nice=</option></term>
271
272 <listitem><para>Runs the service process with the specified
273 nice level. Also see <varname>Nice=</varname> in
274 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
275
276 <xi:include href="version-info.xml" xpointer="v211"/>
277 </listitem>
278 </varlistentry>
279
280 <varlistentry>
281 <term><option>--working-directory=</option></term>
282
283 <listitem><para>Runs the service process with the specified working directory. Also see
284 <varname>WorkingDirectory=</varname> in
285 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
286
287 <xi:include href="version-info.xml" xpointer="v240"/>
288 </listitem>
289 </varlistentry>
290
291 <varlistentry>
292 <term><option>--same-dir</option></term>
293 <term><option>-d</option></term>
294
295 <listitem><para>Similar to <option>--working-directory=</option>, but uses the current working
296 directory of the caller for the service to execute.</para>
297
298 <xi:include href="version-info.xml" xpointer="v240"/></listitem>
299 </varlistentry>
300
301 <varlistentry>
302 <term><option>-E <replaceable>NAME</replaceable>[=<replaceable>VALUE</replaceable>]</option></term>
303 <term><option>--setenv=<replaceable>NAME</replaceable>[=<replaceable>VALUE</replaceable>]</option></term>
304
305 <listitem><para>Runs the service process with the specified environment variable set. This parameter
306 may be used more than once to set multiple variables. When <literal>=</literal> and
307 <replaceable>VALUE</replaceable> are omitted, the value of the variable with the same name in the
308 program environment will be used.</para>
309
310 <para>Also see <varname>Environment=</varname> in
311 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
312
313 <xi:include href="version-info.xml" xpointer="v211"/>
314 </listitem>
315 </varlistentry>
316
317 <varlistentry>
318 <term><option>--pty</option></term>
319 <term><option>-t</option></term>
320
321 <listitem><para>When invoking the command, the transient service connects its standard input, output and error
322 to the terminal <command>systemd-run</command> is invoked on, via a pseudo TTY device. This allows running
323 programs that expect interactive user input/output as services, such as interactive command shells.</para>
324
325 <para>Note that
326 <citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>'s
327 <command>shell</command> command is usually a better alternative for requesting a new, interactive login
328 session on the local host or a local container.</para>
329
330 <para>See below for details on how this switch combines with <option>--pipe</option>.</para>
331
332 <xi:include href="version-info.xml" xpointer="v219"/></listitem>
333 </varlistentry>
334
335 <varlistentry>
336 <term><option>--pipe</option></term>
337 <term><option>-P</option></term>
338
339 <listitem><para>If specified, standard input, output, and error of the transient service are inherited from the
340 <command>systemd-run</command> command itself. This allows <command>systemd-run</command>
341 to be used within shell pipelines.
342 Note that this mode is not suitable for interactive command shells and similar, as the
343 service process will not become a TTY controller when invoked on a terminal. Use <option>--pty</option> instead
344 in that case.</para>
345
346 <para>When both <option>--pipe</option> and <option>--pty</option> are used in combination the more appropriate
347 option is automatically determined and used. Specifically, when invoked with standard input, output and error
348 connected to a TTY <option>--pty</option> is used, and otherwise <option>--pipe</option>.</para>
349
350 <para>When this option is used the original file descriptors <command>systemd-run</command> receives are passed
351 to the service processes as-is. If the service runs with different privileges than
352 <command>systemd-run</command>, this means the service might not be able to re-open the passed file
353 descriptors, due to normal file descriptor access restrictions. If the invoked process is a shell script that
354 uses the <command>echo "hello" >/dev/stderr</command> construct for writing messages to stderr, this might
355 cause problems, as this only works if stderr can be re-opened. To mitigate this use the construct <command>echo
356 "hello" >&amp;2</command> instead, which is mostly equivalent and avoids this pitfall.</para>
357
358 <xi:include href="version-info.xml" xpointer="v235"/></listitem>
359 </varlistentry>
360
361 <varlistentry>
362 <term><option>--shell</option></term>
363 <term><option>-S</option></term>
364
365 <listitem><para>A shortcut for <literal>--pty --same-dir --wait --collect --service-type=exec $SHELL</literal>,
366 i.e. requests an interactive shell in the current working directory, running in service context, accessible
367 with a single switch.</para>
368
369 <xi:include href="version-info.xml" xpointer="v240"/></listitem>
370 </varlistentry>
371
372 <varlistentry>
373 <term><option>--quiet</option></term>
374 <term><option>-q</option></term>
375
376 <listitem><para>Suppresses additional informational output
377 while running. This is particularly useful in combination with
378 <option>--pty</option> when it will suppress the initial
379 message explaining how to terminate the TTY connection.</para>
380
381 <xi:include href="version-info.xml" xpointer="v219"/></listitem>
382 </varlistentry>
383
384 <varlistentry>
385 <term><option>--on-active=</option></term>
386 <term><option>--on-boot=</option></term>
387 <term><option>--on-startup=</option></term>
388 <term><option>--on-unit-active=</option></term>
389 <term><option>--on-unit-inactive=</option></term>
390
391 <listitem><para>Defines a monotonic timer relative to different starting points for starting the specified
392 command. See <varname>OnActiveSec=</varname>, <varname>OnBootSec=</varname>, <varname>OnStartupSec=</varname>,
393 <varname>OnUnitActiveSec=</varname> and <varname>OnUnitInactiveSec=</varname> in
394 <citerefentry><refentrytitle>systemd.timer</refentrytitle><manvolnum>5</manvolnum></citerefentry> for
395 details. These options are shortcuts for <command>--timer-property=</command> with the relevant properties.
396 These options may not be combined with <option>--scope</option> or <option>--pty</option>.</para>
397
398 <xi:include href="version-info.xml" xpointer="v218"/>
399 </listitem>
400 </varlistentry>
401
402 <varlistentry>
403 <term><option>--on-calendar=</option></term>
404
405 <listitem><para>Defines a calendar timer for starting the specified command. See <varname>OnCalendar=</varname>
406 in <citerefentry><refentrytitle>systemd.timer</refentrytitle><manvolnum>5</manvolnum></citerefentry>. This
407 option is a shortcut for <command>--timer-property=OnCalendar=</command>. This option may not be combined with
408 <option>--scope</option> or <option>--pty</option>.</para>
409
410 <xi:include href="version-info.xml" xpointer="v218"/>
411 </listitem>
412 </varlistentry>
413
414 <varlistentry>
415 <term><option>--on-clock-change</option></term>
416 <term><option>--on-timezone-change</option></term>
417
418 <listitem><para>Defines a trigger based on system clock jumps or timezone changes for starting the
419 specified command. See <varname>OnClockChange=</varname> and <varname>OnTimezoneChange=</varname> in
420 <citerefentry><refentrytitle>systemd.timer</refentrytitle><manvolnum>5</manvolnum></citerefentry>. These
421 options are shortcuts for <command>--timer-property=OnClockChange=yes</command> and
422 <command>--timer-property=OnTimezoneChange=yes</command>. These options may not be combined with
423 <option>--scope</option> or <option>--pty</option>.</para>
424
425 <xi:include href="version-info.xml" xpointer="v242"/></listitem>
426 </varlistentry>
427
428 <varlistentry>
429 <term><option>--path-property=</option></term>
430 <term><option>--socket-property=</option></term>
431 <term><option>--timer-property=</option></term>
432
433 <listitem><para>Sets a property on the path, socket, or timer unit that is created. This option is
434 similar to <option>--property=</option>, but applies to the transient path, socket, or timer unit
435 rather than the transient service unit created. This option takes an assignment in the same format as
436 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>'s
437 <command>set-property</command> command. These options may not be combined with
438 <option>--scope</option> or <option>--pty</option>.</para>
439
440 <xi:include href="version-info.xml" xpointer="v218"/>
441 </listitem>
442 </varlistentry>
443
444 <varlistentry>
445 <term><option>--no-block</option></term>
446
447 <listitem>
448 <para>Do not synchronously wait for the unit start operation to finish. If this option is not specified, the
449 start request for the transient unit will be verified, enqueued and <command>systemd-run</command> will wait
450 until the unit's start-up is completed. By passing this argument, it is only verified and enqueued. This
451 option may not be combined with <option>--wait</option>.</para>
452
453 <xi:include href="version-info.xml" xpointer="v220"/>
454 </listitem>
455 </varlistentry>
456
457 <varlistentry>
458 <term><option>--wait</option></term>
459
460 <listitem><para>Synchronously wait for the transient service to terminate. If this option is specified, the
461 start request for the transient unit is verified, enqueued, and waited for. Subsequently the invoked unit is
462 monitored, and it is waited until it is deactivated again (most likely because the specified command
463 completed). On exit, terse information about the unit's runtime is shown, including total runtime (as well as
464 CPU usage, if <option>--property=CPUAccounting=1</option> was set) and the exit code and status of the main
465 process. This output may be suppressed with <option>--quiet</option>. This option may not be combined with
466 <option>--no-block</option>, <option>--scope</option> or the various path, socket, or timer options.</para>
467
468 <xi:include href="version-info.xml" xpointer="v232"/></listitem>
469 </varlistentry>
470
471 <varlistentry>
472 <term><option>-G</option></term>
473 <term><option>--collect</option></term>
474
475 <listitem><para>Unload the transient unit after it completed, even if it failed. Normally, without this option,
476 all units that ran and failed are kept in memory until the user explicitly resets their failure state with
477 <command>systemctl reset-failed</command> or an equivalent command. On the other hand, units that ran
478 successfully are unloaded immediately. If this option is turned on the "garbage collection" of units is more
479 aggressive, and unloads units regardless if they exited successfully or failed. This option is a shortcut for
480 <command>--property=CollectMode=inactive-or-failed</command>, see the explanation for
481 <varname>CollectMode=</varname> in
482 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry> for further
483 information.</para>
484
485 <xi:include href="version-info.xml" xpointer="v236"/></listitem>
486 </varlistentry>
487
488 <varlistentry>
489 <term><option>--ignore-failure</option></term>
490
491 <listitem><para>By default, if the specified command fails the invoked unit will be marked failed
492 (though possibly still unloaded, see <option>--collect=</option>, above), and this is reported in the
493 logs. If this switch is specified this is suppressed and any non-success exit status/code of the
494 command is treated as success.</para>
495
496 <xi:include href="version-info.xml" xpointer="v256"/></listitem>
497 </varlistentry>
498
499 <varlistentry>
500 <term><option>--background=<replaceable>COLOR</replaceable></option></term>
501
502 <listitem><para>Change the terminal background color to the specified ANSI color as long as the
503 session lasts. The color specified should be an ANSI X3.64 SGR background color, i.e. strings such as
504 <literal>40</literal>, <literal>41</literal>, …, <literal>47</literal>, <literal>48;2;…</literal>,
505 <literal>48;5;…</literal>. See <ulink
506 url="https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters">ANSI
507 Escape Code (Wikipedia)</ulink> for details.</para>
508
509 <xi:include href="version-info.xml" xpointer="v256"/>
510 </listitem>
511 </varlistentry>
512
513 <xi:include href="user-system-options.xml" xpointer="user" />
514 <xi:include href="user-system-options.xml" xpointer="system" />
515 <xi:include href="user-system-options.xml" xpointer="host" />
516 <xi:include href="user-system-options.xml" xpointer="machine" />
517
518 <xi:include href="standard-options.xml" xpointer="help" />
519 <xi:include href="standard-options.xml" xpointer="version" />
520 </variablelist>
521
522 <para>All command line arguments after the first non-option argument become part of the command line of
523 the launched process.</para>
524 </refsect1>
525
526 <refsect1>
527 <title>Exit status</title>
528
529 <para>On success, 0 is returned. If <command>systemd-run</command> failed to start the service, a
530 non-zero return value will be returned. If <command>systemd-run</command> waits for the service to
531 terminate, the return value will be propagated from the service. 0 will be returned on success, including
532 all the cases where systemd considers a service to have exited cleanly, see the discussion of
533 <varname>SuccessExitStatus=</varname> in
534 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
535 </para>
536 </refsect1>
537
538 <refsect1>
539 <title>Examples</title>
540
541 <example>
542 <title>Logging environment variables provided by systemd to services</title>
543
544 <programlisting># systemd-run env
545 Running as unit: run-19945.service
546 # journalctl -u run-19945.service
547 Sep 08 07:37:21 bupkis systemd[1]: Starting /usr/bin/env...
548 Sep 08 07:37:21 bupkis systemd[1]: Started /usr/bin/env.
549 Sep 08 07:37:21 bupkis env[19948]: PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
550 Sep 08 07:37:21 bupkis env[19948]: LANG=en_US.UTF-8
551 Sep 08 07:37:21 bupkis env[19948]: BOOT_IMAGE=/vmlinuz-3.11.0-0.rc5.git6.2.fc20.x86_64</programlisting>
552 </example>
553
554 <example>
555 <title>Limiting resources available to a command</title>
556
557 <programlisting># systemd-run -p IOWeight=10 updatedb</programlisting>
558
559 <para>This command invokes the <citerefentry
560 project='man-pages'><refentrytitle>updatedb</refentrytitle><manvolnum>8</manvolnum></citerefentry>
561 tool, but lowers the block I/O weight for it to 10. See
562 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>
563 for more information on the <varname>IOWeight=</varname> property.</para>
564 </example>
565
566 <example>
567 <title>Running commands at a specified time</title>
568
569 <para>The following command will touch a file after 30 seconds.</para>
570
571 <programlisting># date; systemd-run --on-active=30 --timer-property=AccuracySec=100ms /bin/touch /tmp/foo
572 Mon Dec 8 20:44:24 KST 2014
573 Running as unit: run-71.timer
574 Will run service as unit: run-71.service
575 # journalctl -b -u run-71.timer
576 -- Journal begins at Fri 2014-12-05 19:09:21 KST, ends at Mon 2014-12-08 20:44:54 KST. --
577 Dec 08 20:44:38 container systemd[1]: Starting /bin/touch /tmp/foo.
578 Dec 08 20:44:38 container systemd[1]: Started /bin/touch /tmp/foo.
579 # journalctl -b -u run-71.service
580 -- Journal begins at Fri 2014-12-05 19:09:21 KST, ends at Mon 2014-12-08 20:44:54 KST. --
581 Dec 08 20:44:48 container systemd[1]: Starting /bin/touch /tmp/foo...
582 Dec 08 20:44:48 container systemd[1]: Started /bin/touch /tmp/foo.</programlisting>
583 </example>
584
585 <example>
586 <title>Allowing access to the tty</title>
587
588 <para>The following command invokes
589 <citerefentry project='man-pages'><refentrytitle>bash</refentrytitle><manvolnum>1</manvolnum></citerefentry>
590 as a service passing its standard input, output and error to the calling TTY.</para>
591
592 <programlisting># systemd-run -t --send-sighup bash</programlisting>
593 </example>
594
595 <example>
596 <title>Start <command>screen</command> as a user service</title>
597
598 <programlisting>$ systemd-run --scope --user screen
599 Running scope as unit run-r14b0047ab6df45bfb45e7786cc839e76.scope.
600
601 $ screen -ls
602 There is a screen on:
603 492..laptop (Detached)
604 1 Socket in /var/run/screen/S-fatima.
605 </programlisting>
606
607 <para>This starts the <command>screen</command> process as a child of the
608 <command>systemd --user</command> process that was started by
609 <filename>user@.service</filename>, in a scope unit. A
610 <citerefentry><refentrytitle>systemd.scope</refentrytitle><manvolnum>5</manvolnum></citerefentry>
611 unit is used instead of a
612 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
613 unit, because <command>screen</command> will exit when detaching from the terminal,
614 and a service unit would be terminated. Running <command>screen</command>
615 as a user unit has the advantage that it is not part of the session scope.
616 If <varname>KillUserProcesses=yes</varname> is configured in
617 <citerefentry><refentrytitle>logind.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
618 the default, the session scope will be terminated when the user logs
619 out of that session.</para>
620
621 <para>The <filename>user@.service</filename> is started automatically
622 when the user first logs in, and stays around as long as at least one
623 login session is open. After the user logs out of the last session,
624 <filename>user@.service</filename> and all services underneath it
625 are terminated. This behavior is the default, when "lingering" is
626 not enabled for that user. Enabling lingering means that
627 <filename>user@.service</filename> is started automatically during
628 boot, even if the user is not logged in, and that the service is
629 not terminated when the user logs out.</para>
630
631 <para>Enabling lingering allows the user to run processes without being logged in,
632 for example to allow <command>screen</command> to persist after the user logs out,
633 even if the session scope is terminated. In the default configuration, users can
634 enable lingering for themselves:</para>
635
636 <programlisting>$ loginctl enable-linger</programlisting>
637 </example>
638
639 <example>
640 <title>Variable expansion by the manager</title>
641
642 <programlisting>$ systemd-run -t echo "&lt;${INVOCATION_ID}>" '&lt;${INVOCATION_ID}>'
643 &lt;> &lt;5d0149bfa2c34b79bccb13074001eb20>
644 </programlisting>
645
646 <para>The first argument is expanded by the shell (double quotes), but the second one is not expanded
647 by the shell (single quotes).
648 <citerefentry project='man-pages'><refentrytitle>echo</refentrytitle><manvolnum>1</manvolnum></citerefentry>
649 is called with [<literal>/usr/bin/echo</literal>,
650 <literal>&lt;></literal>, <literal>&lt;${INVOCATION_ID}></literal>] as the argument array, and then
651 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>
652 generates <varname>${INVOCATION_ID}</varname> and substitutes it in the command-line. This substitution
653 could not be done on the client side, because the target ID that will be set for the service isn't
654 known before the call is made.</para>
655 </example>
656
657 <example>
658 <title>Variable expansion and output redirection using a shell</title>
659
660 <para>Variable expansion by
661 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>
662 can be disabled with <varname>--expand-environment=no</varname>.</para>
663
664 <para>Disabling variable expansion can be useful if the command to execute contains dollar characters
665 and escaping them would be inconvenient. For example, when a shell is used:</para>
666
667 <programlisting>$ systemd-run --expand-environment=no -t bash \
668 -c 'echo $SHELL $$ >/dev/stdout'
669 /bin/bash 12345
670 </programlisting>
671
672 <para>The last argument is passed verbatim to the
673 <citerefentry project='man-pages'><refentrytitle>bash</refentrytitle><manvolnum>1</manvolnum></citerefentry>
674 shell which is started by the service unit. The shell expands <literal>$SHELL</literal> to the path of
675 the shell, and <literal>$$</literal> to its process number, and then those strings are passed to the
676 <command>echo</command> built-in and printed to standard output (which in this case is connected to the
677 calling terminal).</para>
678 </example>
679
680 <example>
681 <title>Return value</title>
682
683 <programlisting>$ systemd-run --user --wait true
684 $ systemd-run --user --wait -p SuccessExitStatus=11 bash -c 'exit 11'
685 $ systemd-run --user --wait -p SuccessExitStatus=SIGUSR1 --expand-environment=no \
686 bash -c 'kill -SIGUSR1 $$'</programlisting>
687
688 <para>Those three invocations will succeed, i.e. terminate with an exit code of 0.</para>
689 </example>
690 </refsect1>
691
692 <refsect1>
693 <title>See Also</title>
694 <para>
695 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
696 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
697 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
698 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
699 <citerefentry><refentrytitle>systemd.scope</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
700 <citerefentry><refentrytitle>systemd.slice</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
701 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
702 <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
703 <citerefentry><refentrytitle>systemd.timer</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
704 <citerefentry><refentrytitle>systemd-mount</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
705 <citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
706 <citerefentry><refentrytitle>uid0</refentrytitle><manvolnum>1</manvolnum></citerefentry>
707 </para>
708 </refsect1>
709
710 </refentry>