1 <?xml version='
1.0'
?> <!--*- Mode: nxml; nxml-child-indent: 2; indent-tabs-mode: nil -*-->
2 <!DOCTYPE refentry PUBLIC
"-//OASIS//DTD DocBook XML V4.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
6 SPDX-License-Identifier: LGPL-2.1+
8 This file is part of systemd.
10 Copyright 2013 Zbigniew Jędrzejewski-Szmek
12 systemd is free software; you can redistribute it and/or modify it
13 under the terms of the GNU Lesser General Public License as published by
14 the Free Software Foundation; either version 2.1 of the License, or
15 (at your option) any later version.
17 systemd is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License for more details.
22 You should have received a copy of the GNU Lesser General Public License
23 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 <refentry id=
"systemd-run"
27 xmlns:
xi=
"http://www.w3.org/2001/XInclude">
30 <title>systemd-run
</title>
31 <productname>systemd
</productname>
35 <contrib>Developer
</contrib>
36 <firstname>Lennart
</firstname>
37 <surname>Poettering
</surname>
38 <email>lennart@poettering.net
</email>
44 <refentrytitle>systemd-run
</refentrytitle>
45 <manvolnum>1</manvolnum>
49 <refname>systemd-run
</refname>
50 <refpurpose>Run programs in transient scope units, service units, or path-, socket-, or timer-triggered service units
</refpurpose>
55 <command>systemd-run
</command>
56 <arg choice=
"opt" rep=
"repeat">OPTIONS
</arg>
57 <arg choice=
"plain"><replaceable>COMMAND
</replaceable>
58 <arg choice=
"opt" rep=
"repeat">ARGS
</arg>
62 <command>systemd-run
</command>
63 <arg choice=
"opt" rep=
"repeat">OPTIONS
</arg>
64 <arg choice=
"opt" rep=
"repeat">PATH OPTIONS
</arg>
65 <arg choice=
"req"><replaceable>COMMAND
</replaceable></arg>
66 <arg choice=
"opt" rep=
"repeat">ARGS
</arg>
69 <command>systemd-run
</command>
70 <arg choice=
"opt" rep=
"repeat">OPTIONS
</arg>
71 <arg choice=
"opt" rep=
"repeat">SOCKET OPTIONS
</arg>
72 <arg choice=
"req"><replaceable>COMMAND
</replaceable></arg>
73 <arg choice=
"opt" rep=
"repeat">ARGS
</arg>
76 <command>systemd-run
</command>
77 <arg choice=
"opt" rep=
"repeat">OPTIONS
</arg>
78 <arg choice=
"opt" rep=
"repeat">TIMER OPTIONS
</arg>
79 <arg choice=
"req"><replaceable>COMMAND
</replaceable></arg>
80 <arg choice=
"opt" rep=
"repeat">ARGS
</arg>
85 <title>Description
</title>
87 <para><command>systemd-run
</command> may be used to create and start a transient
<filename>.service
</filename> or
88 <filename>.scope
</filename> unit and run the specified
<replaceable>COMMAND
</replaceable> in it. It may also be
89 used to create and start a transient
<filename>.path
</filename>,
<filename>.socket
</filename>, or
90 <filename>.timer
</filename> unit, that activates a
<filename>.service
</filename> unit when elapsing.
</para>
92 <para>If a command is run as transient service unit, it will be started and managed by the service manager like any
93 other service, and thus shows up in the output of
<command>systemctl list-units
</command> like any other unit. It
94 will run in a clean and detached execution environment, with the service manager as its parent process. In this
95 mode,
<command>systemd-run
</command> will start the service asynchronously in the background and return after the
96 command has begun execution (unless
<option>--no-block
</option> or
<option>--wait
</option> are specified, see
99 <para>If a command is run as transient scope unit, it will be executed by
<command>systemd-run
</command> itself as
100 parent process and will thus inherit the execution environment of the caller. However, the processes of the command
101 are managed by the service manager similar to normal services, and will show up in the output of
<command>systemctl
102 list-units
</command>. Execution in this case is synchronous, and will return only when the command finishes. This
103 mode is enabled via the
<option>--scope
</option> switch (see below).
</para>
105 <para>If a command is run with path, socket, or timer options such as
<option>--on-calendar=
</option> (see below),
106 a transient path, socket, or timer unit is created alongside the service unit for the specified command. Only the
107 transient path, socket, or timer unit is started immediately, the transient service unit will be triggered by the
108 path, socket, or timer unit. If the
<option>--unit=
</option> option is specified, the
109 <replaceable>COMMAND
</replaceable> may be omitted. In this case,
<command>systemd-run
</command> creates only a
110 <filename>.path
</filename>,
<filename>.socket
</filename>, or
<filename>.timer
</filename> unit that triggers the
111 specified unit.
</para>
115 <title>Options
</title>
117 <para>The following options are understood:
</para>
121 <term><option>--no-ask-password
</option></term>
123 <listitem><para>Do not query the user for authentication for
124 privileged operations.
</para></listitem>
128 <term><option>--scope
</option></term>
131 <para>Create a transient
<filename>.scope
</filename> unit instead of the default transient
132 <filename>.service
</filename> unit (see above).
138 <term><option>--unit=
</option></term>
140 <listitem><para>Use this unit name instead of an automatically
141 generated one.
</para></listitem>
145 <term><option>--property=
</option></term>
146 <term><option>-p
</option></term>
148 <listitem><para>Sets a property on the scope or service unit that is created. This option takes an assignment
149 in the same format as
150 <citerefentry><refentrytitle>systemctl
</refentrytitle><manvolnum>1</manvolnum></citerefentry>'s
151 <command>set-property
</command> command.
</para>
156 <term><option>--description=
</option></term>
158 <listitem><para>Provide a description for the service, scope, path, socket, or timer unit. If not specified,
159 the command itself will be used as a description. See
<varname>Description=
</varname> in
160 <citerefentry><refentrytitle>systemd.unit
</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
165 <term><option>--slice=
</option></term>
167 <listitem><para>Make the new
<filename>.service
</filename> or
<filename>.scope
</filename> unit part of the
168 specified slice, instead of
<filename>system.slice
</filename>.
</para>
173 <term><option>-r
</option></term>
174 <term><option>--remain-after-exit
</option></term>
176 <listitem><para>After the service process has terminated, keep the service around until it is explicitly
177 stopped. This is useful to collect runtime information about the service after it finished running. Also see
178 <varname>RemainAfterExit=
</varname> in
179 <citerefentry><refentrytitle>systemd.service
</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
185 <term><option>--send-sighup
</option></term>
187 <listitem><para>When terminating the scope or service unit, send a SIGHUP immediately after SIGTERM. This is
188 useful to indicate to shells and shell-like processes that the connection has been severed. Also see
189 <varname>SendSIGHUP=
</varname> in
190 <citerefentry><refentrytitle>systemd.kill
</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
196 <term><option>--service-type=
</option></term>
198 <listitem><para>Sets the service type. Also see
199 <varname>Type=
</varname> in
200 <citerefentry><refentrytitle>systemd.service
</refentrytitle><manvolnum>5</manvolnum></citerefentry>. This
201 option has no effect in conjunction with
202 <option>--scope
</option>. Defaults to
203 <constant>simple
</constant>.
</para>
208 <term><option>--uid=
</option></term>
209 <term><option>--gid=
</option></term>
211 <listitem><para>Runs the service process under the specified UNIX user and group. Also see
212 <varname>User=
</varname> and
<varname>Group=
</varname> in
213 <citerefentry><refentrytitle>systemd.exec
</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
</para>
218 <term><option>--nice=
</option></term>
220 <listitem><para>Runs the service process with the specified
221 nice level. Also see
<varname>Nice=
</varname> in
222 <citerefentry><refentrytitle>systemd.exec
</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
</para>
227 <term><option>-E
<replaceable>NAME
</replaceable>=
<replaceable>VALUE
</replaceable></option></term>
228 <term><option>--setenv=
<replaceable>NAME
</replaceable>=
<replaceable>VALUE
</replaceable></option></term>
230 <listitem><para>Runs the service process with the specified environment variable set.
231 Also see
<varname>Environment=
</varname> in
232 <citerefentry><refentrytitle>systemd.exec
</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
</para>
237 <term><option>--pty
</option></term>
238 <term><option>-t
</option></term>
240 <listitem><para>When invoking the command, the transient service connects its standard input, output and error
241 to the terminal
<command>systemd-run
</command> is invoked on, via a pseudo TTY device. This allows running
242 programs that expect interactive user input/output as services, such as interactive command shells.
</para>
245 <citerefentry><refentrytitle>machinectl
</refentrytitle><manvolnum>1</manvolnum></citerefentry>'s
246 <command>shell
</command> command is usually a better alternative for requesting a new, interactive login
247 session on the local host or a local container.
</para>
249 <para>See below for details on how this switch combines with
<option>--pipe
</option>.
</para></listitem>
253 <term><option>--pipe
</option></term>
254 <term><option>-P
</option></term>
256 <listitem><para>If specified, standard input, output, and error of the transient service are inherited from the
257 <command>systemd-run
</command> command itself. This allows
<command>systemd-run
</command>
258 to be used within shell pipelines.
259 Note that this mode is not suitable for interactive command shells and similar, as the
260 service process will not become a TTY controller when invoked on a terminal. Use
<option>--pty
</option> instead
263 <para>When both
<option>--pipe
</option> and
<option>--pty
</option> are used in combination the more appropriate
264 option is automatically determined and used. Specifically, when invoked with standard input, output and error
265 connected to a TTY
<option>--pty
</option> is used, and otherwise
<option>--pipe
</option>.
</para>
267 <para>When this option is used the original file descriptors
<command>systemd-run
</command> receives are passed
268 to the service processes as-is. If the service runs with different privileges than
269 <command>systemd-run
</command>, this means the service might not be able to re-open the passed file
270 descriptors, due to normal file descriptor access restrictions. If the invoked process is a shell script that
271 uses the
<command>echo
"hello" > /dev/stderr
</command> construct for writing messages to stderr, this might
272 cause problems, as this only works if stderr can be re-opened. To mitigate this use the construct
<command>echo
273 "hello" >&2</command> instead, which is mostly equivalent and avoids this pitfall.
</para></listitem>
277 <term><option>--quiet
</option></term>
278 <term><option>-q
</option></term>
280 <listitem><para>Suppresses additional informational output
281 while running. This is particularly useful in combination with
282 <option>--pty
</option> when it will suppress the initial
283 message explaining how to terminate the TTY connection.
</para></listitem>
287 <term><option>--on-active=
</option></term>
288 <term><option>--on-boot=
</option></term>
289 <term><option>--on-startup=
</option></term>
290 <term><option>--on-unit-active=
</option></term>
291 <term><option>--on-unit-inactive=
</option></term>
293 <listitem><para>Defines a monotonic timer relative to different starting points for starting the specified
294 command. See
<varname>OnActiveSec=
</varname>,
<varname>OnBootSec=
</varname>,
<varname>OnStartupSec=
</varname>,
295 <varname>OnUnitActiveSec=
</varname> and
<varname>OnUnitInactiveSec=
</varname> in
296 <citerefentry><refentrytitle>systemd.timer
</refentrytitle><manvolnum>5</manvolnum></citerefentry> for
297 details. These options are shortcuts for
<command>--timer-property=
</command> with the relevant properties.
298 These options may not be combined with
<option>--scope
</option> or
<option>--pty
</option>.
</para>
303 <term><option>--on-calendar=
</option></term>
305 <listitem><para>Defines a calendar timer for starting the specified command. See
<varname>OnCalendar=
</varname>
306 in
<citerefentry><refentrytitle>systemd.timer
</refentrytitle><manvolnum>5</manvolnum></citerefentry>. This
307 option is a shortcut for
<command>--timer-property=OnCalendar=
</command>. This option may not be combined with
308 <option>--scope
</option> or
<option>--pty
</option>.
</para>
313 <term><option>--path-property=
</option></term>
314 <term><option>--socket-property=
</option></term>
315 <term><option>--timer-property=
</option></term>
317 <listitem><para>Sets a property on the path, socket, or timer unit that is created. This option is similar to
318 <option>--property=
</option> but applies to the transient path, socket, or timer unit rather than the
319 transient service unit created. This option takes an assignment in the same format as
320 <citerefentry><refentrytitle>systemctl
</refentrytitle><manvolnum>1</manvolnum></citerefentry>'s
321 <command>set-property
</command> command. These options may not be combined with
322 <option>--scope
</option> or
<option>--pty
</option>.
</para>
327 <term><option>--no-block
</option></term>
330 <para>Do not synchronously wait for the unit start operation to finish. If this option is not specified, the
331 start request for the transient unit will be verified, enqueued and
<command>systemd-run
</command> will wait
332 until the unit's start-up is completed. By passing this argument, it is only verified and enqueued. This
333 option may not be combined with
<option>--wait
</option>.
</para>
338 <term><option>--wait
</option></term>
340 <listitem><para>Synchronously wait for the transient service to terminate. If this option is specified, the
341 start request for the transient unit is verified, enqueued, and waited for. Subsequently the invoked unit is
342 monitored, and it is waited until it is deactivated again (most likely because the specified command
343 completed). On exit, terse information about the unit's runtime is shown, including total runtime (as well as
344 CPU usage, if
<option>--property=CPUAccounting=
1</option> was set) and the exit code and status of the main
345 process. This output may be suppressed with
<option>--quiet
</option>. This option may not be combined with
346 <option>--no-block
</option>,
<option>--scope
</option> or the various path, socket, or timer options.
</para></listitem>
350 <term><option>-G
</option></term>
351 <term><option>--collect
</option></term>
353 <listitem><para>Unload the transient unit after it completed, even if it failed. Normally, without this option,
354 all units that ran and failed are kept in memory until the user explicitly resets their failure state with
355 <command>systemctl reset-failed
</command> or an equivalent command. On the other hand, units that ran
356 successfully are unloaded immediately. If this option is turned on the
"garbage collection" of units is more
357 aggressive, and unloads units regardless if they exited successfully or failed. This option is a shortcut for
358 <command>--property=CollectMode=inactive-or-failed
</command>, see the explanation for
359 <varname>CollectMode=
</varname> in
360 <citerefentry><refentrytitle>systemd.unit
</refentrytitle><manvolnum>5</manvolnum></citerefentry> for further
361 information.
</para></listitem>
364 <xi:include href=
"user-system-options.xml" xpointer=
"user" />
365 <xi:include href=
"user-system-options.xml" xpointer=
"system" />
366 <xi:include href=
"user-system-options.xml" xpointer=
"host" />
367 <xi:include href=
"user-system-options.xml" xpointer=
"machine" />
369 <xi:include href=
"standard-options.xml" xpointer=
"help" />
370 <xi:include href=
"standard-options.xml" xpointer=
"version" />
373 <para>All command line arguments after the first non-option
374 argument become part of the command line of the launched
375 process. If a command is run as service unit, the first argument
376 needs to be an absolute program path.
</para>
380 <title>Exit status
</title>
382 <para>On success,
0 is returned, a non-zero failure
383 code otherwise.
</para>
387 <title>Examples
</title>
390 <title>Logging environment variables provided by systemd to services
</title>
392 <programlisting># systemd-run env
393 Running as unit: run-
19945.service
394 # journalctl -u run-
19945.service
395 Sep
08 07:
37:
21 bupkis systemd[
1]: Starting /usr/bin/env...
396 Sep
08 07:
37:
21 bupkis systemd[
1]: Started /usr/bin/env.
397 Sep
08 07:
37:
21 bupkis env[
19948]: PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
398 Sep
08 07:
37:
21 bupkis env[
19948]: LANG=en_US.UTF-
8
399 Sep
08 07:
37:
21 bupkis env[
19948]: BOOT_IMAGE=/vmlinuz-
3.11.0-
0.rc5.git6.2.fc20.x86_64
</programlisting>
403 <title>Limiting resources available to a command
</title>
405 <programlisting># systemd-run -p BlockIOWeight=
10 updatedb
</programlisting>
407 <para>This command invokes the
408 <citerefentry project='man-pages'
><refentrytitle>updatedb
</refentrytitle><manvolnum>8</manvolnum></citerefentry>
409 tool, but lowers the block I/O weight for it to
10. See
410 <citerefentry><refentrytitle>systemd.resource-control
</refentrytitle><manvolnum>5</manvolnum></citerefentry>
411 for more information on the
<varname>BlockIOWeight=
</varname>
416 <title>Running commands at a specified time
</title>
418 <para>The following command will touch a file after
30 seconds.
</para>
420 <programlisting># date; systemd-run --on-active=
30 --timer-property=AccuracySec=
100ms /bin/touch /tmp/foo
421 Mon Dec
8 20:
44:
24 KST
2014
422 Running as unit: run-
71.timer
423 Will run service as unit: run-
71.service
424 # journalctl -b -u run-
71.timer
425 -- Logs begin at Fri
2014-
12-
05 19:
09:
21 KST, end at Mon
2014-
12-
08 20:
44:
54 KST. --
426 Dec
08 20:
44:
38 container systemd[
1]: Starting /bin/touch /tmp/foo.
427 Dec
08 20:
44:
38 container systemd[
1]: Started /bin/touch /tmp/foo.
428 # journalctl -b -u run-
71.service
429 -- Logs begin at Fri
2014-
12-
05 19:
09:
21 KST, end at Mon
2014-
12-
08 20:
44:
54 KST. --
430 Dec
08 20:
44:
48 container systemd[
1]: Starting /bin/touch /tmp/foo...
431 Dec
08 20:
44:
48 container systemd[
1]: Started /bin/touch /tmp/foo.
</programlisting>
435 <title>Allowing access to the tty
</title>
437 <para>The following command invokes
<filename>/bin/bash
</filename> as a service
438 passing its standard input, output and error to the calling TTY.
</para>
440 <programlisting># systemd-run -t --send-sighup /bin/bash
</programlisting>
444 <title>Start
<command>screen
</command> as a user service
</title>
446 <programlisting>$ systemd-run --scope --user screen
447 Running scope as unit run-r14b0047ab6df45bfb45e7786cc839e76.scope.
450 There is a screen on:
451 492..laptop (Detached)
452 1 Socket in /var/run/screen/S-fatima.
455 <para>This starts the
<command>screen
</command> process as a child of the
456 <command>systemd --user
</command> process that was started by
457 <filename>user@.service
</filename>, in a scope unit. A
458 <citerefentry><refentrytitle>systemd.scope
</refentrytitle><manvolnum>5</manvolnum></citerefentry>
459 unit is used instead of a
460 <citerefentry><refentrytitle>systemd.service
</refentrytitle><manvolnum>5</manvolnum></citerefentry>
461 unit, because
<command>screen
</command> will exit when detaching from the terminal,
462 and a service unit would be terminated. Running
<command>screen
</command>
463 as a user unit has the advantage that it is not part of the session scope.
464 If
<varname>KillUserProcesses=yes
</varname> is configured in
465 <citerefentry><refentrytitle>logind.conf
</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
466 the default, the session scope will be terminated when the user logs
467 out of that session.
</para>
469 <para>The
<filename>user@.service
</filename> is started automatically
470 when the user first logs in, and stays around as long as at least one
471 login session is open. After the user logs out of the last session,
472 <filename>user@.service
</filename> and all services underneath it
473 are terminated. This behavior is the default, when
"lingering" is
474 not enabled for that user. Enabling lingering means that
475 <filename>user@.service
</filename> is started automatically during
476 boot, even if the user is not logged in, and that the service is
477 not terminated when the user logs out.
</para>
479 <para>Enabling lingering allows the user to run processes without being logged in,
480 for example to allow
<command>screen
</command> to persist after the user logs out,
481 even if the session scope is terminated. In the default configuration, users can
482 enable lingering for themselves:
</para>
484 <programlisting>$ loginctl enable-linger
</programlisting>
489 <title>See Also
</title>
491 <citerefentry><refentrytitle>systemd
</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
492 <citerefentry><refentrytitle>systemctl
</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
493 <citerefentry><refentrytitle>systemd.unit
</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
494 <citerefentry><refentrytitle>systemd.service
</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
495 <citerefentry><refentrytitle>systemd.scope
</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
496 <citerefentry><refentrytitle>systemd.slice
</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
497 <citerefentry><refentrytitle>systemd.exec
</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
498 <citerefentry><refentrytitle>systemd.resource-control
</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
499 <citerefentry><refentrytitle>systemd.timer
</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
500 <citerefentry><refentrytitle>systemd-mount
</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
501 <citerefentry><refentrytitle>machinectl
</refentrytitle><manvolnum>1</manvolnum></citerefentry>