]> git.ipfire.org Git - thirdparty/systemd.git/blob - man/systemd-run.xml
verify: use manager_load_startable_unit_or_warn() to load units for verification
[thirdparty/systemd.git] / man / systemd-run.xml
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">
4
5 <!--
6 SPDX-License-Identifier: LGPL-2.1+
7
8 This file is part of systemd.
9
10 Copyright 2013 Zbigniew Jędrzejewski-Szmek
11
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.
16
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.
21
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/>.
24 -->
25
26 <refentry id="systemd-run"
27 xmlns:xi="http://www.w3.org/2001/XInclude">
28
29 <refentryinfo>
30 <title>systemd-run</title>
31 <productname>systemd</productname>
32
33 <authorgroup>
34 <author>
35 <contrib>Developer</contrib>
36 <firstname>Lennart</firstname>
37 <surname>Poettering</surname>
38 <email>lennart@poettering.net</email>
39 </author>
40 </authorgroup>
41 </refentryinfo>
42
43 <refmeta>
44 <refentrytitle>systemd-run</refentrytitle>
45 <manvolnum>1</manvolnum>
46 </refmeta>
47
48 <refnamediv>
49 <refname>systemd-run</refname>
50 <refpurpose>Run programs in transient scope units, service units, or path-, socket-, or timer-triggered service units</refpurpose>
51 </refnamediv>
52
53 <refsynopsisdiv>
54 <cmdsynopsis>
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>
59 </arg>
60 </cmdsynopsis>
61 <cmdsynopsis>
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>
67 </cmdsynopsis>
68 <cmdsynopsis>
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>
74 </cmdsynopsis>
75 <cmdsynopsis>
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>
81 </cmdsynopsis>
82 </refsynopsisdiv>
83
84 <refsect1>
85 <title>Description</title>
86
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>
91
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
97 below).</para>
98
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>
104
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>
112 </refsect1>
113
114 <refsect1>
115 <title>Options</title>
116
117 <para>The following options are understood:</para>
118
119 <variablelist>
120 <varlistentry>
121 <term><option>--no-ask-password</option></term>
122
123 <listitem><para>Do not query the user for authentication for
124 privileged operations.</para></listitem>
125 </varlistentry>
126
127 <varlistentry>
128 <term><option>--scope</option></term>
129
130 <listitem>
131 <para>Create a transient <filename>.scope</filename> unit instead of the default transient
132 <filename>.service</filename> unit (see above).
133 </para>
134 </listitem>
135 </varlistentry>
136
137 <varlistentry>
138 <term><option>--unit=</option></term>
139
140 <listitem><para>Use this unit name instead of an automatically
141 generated one.</para></listitem>
142 </varlistentry>
143
144 <varlistentry>
145 <term><option>--property=</option></term>
146 <term><option>-p</option></term>
147
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>
152 </listitem>
153 </varlistentry>
154
155 <varlistentry>
156 <term><option>--description=</option></term>
157
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>.
161 </para></listitem>
162 </varlistentry>
163
164 <varlistentry>
165 <term><option>--slice=</option></term>
166
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>
169 </listitem>
170 </varlistentry>
171
172 <varlistentry>
173 <term><option>-r</option></term>
174 <term><option>--remain-after-exit</option></term>
175
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>.
180 </para>
181 </listitem>
182 </varlistentry>
183
184 <varlistentry>
185 <term><option>--send-sighup</option></term>
186
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>.
191 </para>
192 </listitem>
193 </varlistentry>
194
195 <varlistentry>
196 <term><option>--service-type=</option></term>
197
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>
204 </listitem>
205 </varlistentry>
206
207 <varlistentry>
208 <term><option>--uid=</option></term>
209 <term><option>--gid=</option></term>
210
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>
214 </listitem>
215 </varlistentry>
216
217 <varlistentry>
218 <term><option>--nice=</option></term>
219
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>
223 </listitem>
224 </varlistentry>
225
226 <varlistentry>
227 <term><option>-E <replaceable>NAME</replaceable>=<replaceable>VALUE</replaceable></option></term>
228 <term><option>--setenv=<replaceable>NAME</replaceable>=<replaceable>VALUE</replaceable></option></term>
229
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>
233 </listitem>
234 </varlistentry>
235
236 <varlistentry>
237 <term><option>--pty</option></term>
238 <term><option>-t</option></term>
239
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>
243
244 <para>Note that
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>
248
249 <para>See below for details on how this switch combines with <option>--pipe</option>.</para></listitem>
250 </varlistentry>
251
252 <varlistentry>
253 <term><option>--pipe</option></term>
254 <term><option>-P</option></term>
255
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
261 in that case.</para>
262
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>
266
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" >&amp;2</command> instead, which is mostly equivalent and avoids this pitfall.</para></listitem>
274 </varlistentry>
275
276 <varlistentry>
277 <term><option>--quiet</option></term>
278 <term><option>-q</option></term>
279
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>
284 </varlistentry>
285
286 <varlistentry>
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>
292
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>
299 </listitem>
300 </varlistentry>
301
302 <varlistentry>
303 <term><option>--on-calendar=</option></term>
304
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>
309 </listitem>
310 </varlistentry>
311
312 <varlistentry>
313 <term><option>--path-property=</option></term>
314 <term><option>--socket-property=</option></term>
315 <term><option>--timer-property=</option></term>
316
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>
323 </listitem>
324 </varlistentry>
325
326 <varlistentry>
327 <term><option>--no-block</option></term>
328
329 <listitem>
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>
334 </listitem>
335 </varlistentry>
336
337 <varlistentry>
338 <term><option>--wait</option></term>
339
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>
347 </varlistentry>
348
349 <varlistentry>
350 <term><option>-G</option></term>
351 <term><option>--collect</option></term>
352
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>
362 </varlistentry>
363
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" />
368
369 <xi:include href="standard-options.xml" xpointer="help" />
370 <xi:include href="standard-options.xml" xpointer="version" />
371 </variablelist>
372
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>
377 </refsect1>
378
379 <refsect1>
380 <title>Exit status</title>
381
382 <para>On success, 0 is returned, a non-zero failure
383 code otherwise.</para>
384 </refsect1>
385
386 <refsect1>
387 <title>Examples</title>
388
389 <example>
390 <title>Logging environment variables provided by systemd to services</title>
391
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>
400 </example>
401
402 <example>
403 <title>Limiting resources available to a command</title>
404
405 <programlisting># systemd-run -p BlockIOWeight=10 updatedb</programlisting>
406
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>
412 property.</para>
413 </example>
414
415 <example>
416 <title>Running commands at a specified time</title>
417
418 <para>The following command will touch a file after 30 seconds.</para>
419
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>
432 </example>
433
434 <example>
435 <title>Allowing access to the tty</title>
436
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>
439
440 <programlisting># systemd-run -t --send-sighup /bin/bash</programlisting>
441 </example>
442
443 <example>
444 <title>Start <command>screen</command> as a user service</title>
445
446 <programlisting>$ systemd-run --scope --user screen
447 Running scope as unit run-r14b0047ab6df45bfb45e7786cc839e76.scope.
448
449 $ screen -ls
450 There is a screen on:
451 492..laptop (Detached)
452 1 Socket in /var/run/screen/S-fatima.
453 </programlisting>
454
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>
468
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>
478
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>
483
484 <programlisting>$ loginctl enable-linger</programlisting>
485 </example>
486 </refsect1>
487
488 <refsect1>
489 <title>See Also</title>
490 <para>
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>
502 </para>
503 </refsect1>
504
505 </refentry>