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