<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="API documentation for the Rust `process` mod in crate `std`."><meta name="keywords" content="rust, rustlang, rust-lang, process"><title>std::process - Rust</title><link rel="stylesheet" type="text/css" href="../../normalize.css"><link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css"><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script src="../../storage.js"></script><link rel="shortcut icon" href="https://doc.rust-lang.org/favicon.ico"></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><a href='../../std/index.html'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png' alt='logo' width='100'></a><p class='location'>Module process</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li><li><a href="#functions">Functions</a></li></ul></div><p class='location'><a href='../index.html'>std</a></p><script>window.sidebarCurrent = {name: 'process', ty: 'mod', relpath: '../'};</script><script defer src="../sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><input class="search-input" name="search" autocomplete="off" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><a id="settings-menu" href="../../settings.html"><img src="../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='in-band'>Module <a href='../index.html'>std</a>::<wbr><a class="mod" href=''>process</a></span><span class='out-of-band'><span class='since' title='Stable since Rust version 1.0.0'>1.0.0</span><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>−</span>]</a></span><a class='srclink' href='../../src/std/process.rs.html#11-1957' title='goto source code'>[src]</a></span></h1><div class='docblock'><p>A module for working with processes.</p> <p>This module is mostly concerned with spawning and interacting with child processes, but it also provides <a href="fn.abort.html"><code>abort</code></a> and <a href="fn.exit.html"><code>exit</code></a> for terminating the current process.</p> <h1 id="spawning-a-process" class="section-header"><a href="#spawning-a-process">Spawning a process</a></h1> <p>The <a href="struct.Command.html"><code>Command</code></a> struct is used to configure and spawn processes:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">process</span>::<span class="ident">Command</span>; <span class="kw">let</span> <span class="ident">output</span> <span class="op">=</span> <span class="ident">Command</span>::<span class="ident">new</span>(<span class="string">"echo"</span>) .<span class="ident">arg</span>(<span class="string">"Hello world"</span>) .<span class="ident">output</span>() .<span class="ident">expect</span>(<span class="string">"Failed to execute command"</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">b"Hello world\n"</span>, <span class="ident">output</span>.<span class="ident">stdout</span>.<span class="ident">as_slice</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Aprocess%3A%3ACommand%3B%0A%0Alet%20output%20%3D%20Command%3A%3Anew(%22echo%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.arg(%22Hello%20world%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.output()%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.expect(%22Failed%20to%20execute%20command%22)%3B%0A%0Aassert_eq!(b%22Hello%20world%5Cn%22%2C%20output.stdout.as_slice())%3B%0A%7D">Run</a></pre> <p>Several methods on <a href="struct.Command.html"><code>Command</code></a>, such as <a href="struct.Command.html#method.spawn"><code>spawn</code></a> or <a href="struct.Command.html#method.output"><code>output</code></a>, can be used to spawn a process. In particular, <a href="struct.Command.html#method.output"><code>output</code></a> spawns the child process and waits until the process terminates, while <a href="struct.Command.html#method.spawn"><code>spawn</code></a> will return a <a href="struct.Child.html"><code>Child</code></a> that represents the spawned child process.</p> <h1 id="handling-io" class="section-header"><a href="#handling-io">Handling I/O</a></h1> <p>The <a href="struct.Command.html#method.stdout"><code>stdout</code></a>, <a href="struct.Command.html#method.stdin"><code>stdin</code></a>, and <a href="struct.Command.html#method.stderr"><code>stderr</code></a> of a child process can be configured by passing an <a href="struct.Stdio.html"><code>Stdio</code></a> to the corresponding method on <a href="struct.Command.html"><code>Command</code></a>. Once spawned, they can be accessed from the <a href="struct.Child.html"><code>Child</code></a>. For example, piping output from one command into another command can be done like so:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">process</span>::{<span class="ident">Command</span>, <span class="ident">Stdio</span>}; <span class="comment">// stdout must be configured with `Stdio::piped` in order to use</span> <span class="comment">// `echo_child.stdout`</span> <span class="kw">let</span> <span class="ident">echo_child</span> <span class="op">=</span> <span class="ident">Command</span>::<span class="ident">new</span>(<span class="string">"echo"</span>) .<span class="ident">arg</span>(<span class="string">"Oh no, a tpyo!"</span>) .<span class="ident">stdout</span>(<span class="ident">Stdio</span>::<span class="ident">piped</span>()) .<span class="ident">spawn</span>() .<span class="ident">expect</span>(<span class="string">"Failed to start echo process"</span>); <span class="comment">// Note that `echo_child` is moved here, but we won't be needing</span> <span class="comment">// `echo_child` anymore</span> <span class="kw">let</span> <span class="ident">echo_out</span> <span class="op">=</span> <span class="ident">echo_child</span>.<span class="ident">stdout</span>.<span class="ident">expect</span>(<span class="string">"Failed to open echo stdout"</span>); <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">sed_child</span> <span class="op">=</span> <span class="ident">Command</span>::<span class="ident">new</span>(<span class="string">"sed"</span>) .<span class="ident">arg</span>(<span class="string">"s/tpyo/typo/"</span>) .<span class="ident">stdin</span>(<span class="ident">Stdio</span>::<span class="ident">from</span>(<span class="ident">echo_out</span>)) .<span class="ident">stdout</span>(<span class="ident">Stdio</span>::<span class="ident">piped</span>()) .<span class="ident">spawn</span>() .<span class="ident">expect</span>(<span class="string">"Failed to start sed process"</span>); <span class="kw">let</span> <span class="ident">output</span> <span class="op">=</span> <span class="ident">sed_child</span>.<span class="ident">wait_with_output</span>().<span class="ident">expect</span>(<span class="string">"Failed to wait on sed"</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">b"Oh no, a typo!\n"</span>, <span class="ident">output</span>.<span class="ident">stdout</span>.<span class="ident">as_slice</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Aprocess%3A%3A%7BCommand%2C%20Stdio%7D%3B%0A%0A%2F%2F%20stdout%20must%20be%20configured%20with%20%60Stdio%3A%3Apiped%60%20in%20order%20to%20use%0A%2F%2F%20%60echo_child.stdout%60%0Alet%20echo_child%20%3D%20Command%3A%3Anew(%22echo%22)%0A%20%20%20%20.arg(%22Oh%20no%2C%20a%20tpyo!%22)%0A%20%20%20%20.stdout(Stdio%3A%3Apiped())%0A%20%20%20%20.spawn()%0A%20%20%20%20.expect(%22Failed%20to%20start%20echo%20process%22)%3B%0A%0A%2F%2F%20Note%20that%20%60echo_child%60%20is%20moved%20here%2C%20but%20we%20won't%20be%20needing%0A%2F%2F%20%60echo_child%60%20anymore%0Alet%20echo_out%20%3D%20echo_child.stdout.expect(%22Failed%20to%20open%20echo%20stdout%22)%3B%0A%0Alet%20mut%20sed_child%20%3D%20Command%3A%3Anew(%22sed%22)%0A%20%20%20%20.arg(%22s%2Ftpyo%2Ftypo%2F%22)%0A%20%20%20%20.stdin(Stdio%3A%3Afrom(echo_out))%0A%20%20%20%20.stdout(Stdio%3A%3Apiped())%0A%20%20%20%20.spawn()%0A%20%20%20%20.expect(%22Failed%20to%20start%20sed%20process%22)%3B%0A%0Alet%20output%20%3D%20sed_child.wait_with_output().expect(%22Failed%20to%20wait%20on%20sed%22)%3B%0Aassert_eq!(b%22Oh%20no%2C%20a%20typo!%5Cn%22%2C%20output.stdout.as_slice())%3B%0A%7D">Run</a></pre> <p>Note that <a href="struct.ChildStderr.html"><code>ChildStderr</code></a> and <a href="struct.ChildStdout.html"><code>ChildStdout</code></a> implement <a href="../io/trait.Read.html"><code>Read</code></a> and <a href="struct.ChildStdin.html"><code>ChildStdin</code></a> implements <a href="../io/trait.Write.html"><code>Write</code></a>:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">process</span>::{<span class="ident">Command</span>, <span class="ident">Stdio</span>}; <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::<span class="ident">Write</span>; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">child</span> <span class="op">=</span> <span class="ident">Command</span>::<span class="ident">new</span>(<span class="string">"/bin/cat"</span>) .<span class="ident">stdin</span>(<span class="ident">Stdio</span>::<span class="ident">piped</span>()) .<span class="ident">stdout</span>(<span class="ident">Stdio</span>::<span class="ident">piped</span>()) .<span class="ident">spawn</span>() .<span class="ident">expect</span>(<span class="string">"failed to execute child"</span>); { <span class="comment">// limited borrow of stdin</span> <span class="kw">let</span> <span class="ident">stdin</span> <span class="op">=</span> <span class="ident">child</span>.<span class="ident">stdin</span>.<span class="ident">as_mut</span>().<span class="ident">expect</span>(<span class="string">"failed to get stdin"</span>); <span class="ident">stdin</span>.<span class="ident">write_all</span>(<span class="string">b"test"</span>).<span class="ident">expect</span>(<span class="string">"failed to write to stdin"</span>); } <span class="kw">let</span> <span class="ident">output</span> <span class="op">=</span> <span class="ident">child</span> .<span class="ident">wait_with_output</span>() .<span class="ident">expect</span>(<span class="string">"failed to wait on child"</span>); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">b"test"</span>, <span class="ident">output</span>.<span class="ident">stdout</span>.<span class="ident">as_slice</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Aprocess%3A%3A%7BCommand%2C%20Stdio%7D%3B%0Ause%20std%3A%3Aio%3A%3AWrite%3B%0A%0Alet%20mut%20child%20%3D%20Command%3A%3Anew(%22%2Fbin%2Fcat%22)%0A%20%20%20%20.stdin(Stdio%3A%3Apiped())%0A%20%20%20%20.stdout(Stdio%3A%3Apiped())%0A%20%20%20%20.spawn()%0A%20%20%20%20.expect(%22failed%20to%20execute%20child%22)%3B%0A%0A%7B%0A%20%20%20%20%2F%2F%20limited%20borrow%20of%20stdin%0A%20%20%20%20let%20stdin%20%3D%20child.stdin.as_mut().expect(%22failed%20to%20get%20stdin%22)%3B%0A%20%20%20%20stdin.write_all(b%22test%22).expect(%22failed%20to%20write%20to%20stdin%22)%3B%0A%7D%0A%0Alet%20output%20%3D%20child%0A%20%20%20%20.wait_with_output()%0A%20%20%20%20.expect(%22failed%20to%20wait%20on%20child%22)%3B%0A%0Aassert_eq!(b%22test%22%2C%20output.stdout.as_slice())%3B%0A%7D">Run</a></pre> </div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2> <table> <tr class=' module-item'> <td><a class="struct" href="struct.Child.html" title='struct std::process::Child'>Child</a></td> <td class='docblock-short'> <p>Representation of a running or exited child process.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.ChildStderr.html" title='struct std::process::ChildStderr'>ChildStderr</a></td> <td class='docblock-short'> <p>A handle to a child process's stderr.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.ChildStdin.html" title='struct std::process::ChildStdin'>ChildStdin</a></td> <td class='docblock-short'> <p>A handle to a child process's standard input (stdin).</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.ChildStdout.html" title='struct std::process::ChildStdout'>ChildStdout</a></td> <td class='docblock-short'> <p>A handle to a child process's standard output (stdout).</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.Command.html" title='struct std::process::Command'>Command</a></td> <td class='docblock-short'> <p>A process builder, providing fine-grained control over how a new process should be spawned.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.ExitStatus.html" title='struct std::process::ExitStatus'>ExitStatus</a></td> <td class='docblock-short'> <p>Describes the result of a process after it has terminated.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.Output.html" title='struct std::process::Output'>Output</a></td> <td class='docblock-short'> <p>The output of a finished process.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.Stdio.html" title='struct std::process::Stdio'>Stdio</a></td> <td class='docblock-short'> <p>Describes what to do with a standard I/O stream for a child process when passed to the <a href="struct.Command.html#method.stdin"><code>stdin</code></a>, <a href="struct.Command.html#method.stdout"><code>stdout</code></a>, and <a href="struct.Command.html#method.stderr"><code>stderr</code></a> methods of <a href="struct.Command.html"><code>Command</code></a>.</p> </td> </tr> <tr class='unstable module-item'> <td><a class="struct" href="struct.ExitCode.html" title='struct std::process::ExitCode'>ExitCode</a></td> <td class='docblock-short'> [<div class='stab unstable'>Experimental</div>] <p>This type represents the status code a process can return to its parent under normal termination.</p> </td> </tr></table><h2 id='traits' class='section-header'><a href="#traits">Traits</a></h2> <table> <tr class='unstable module-item'> <td><a class="trait" href="trait.Termination.html" title='trait std::process::Termination'>Termination</a></td> <td class='docblock-short'> [<div class='stab unstable'>Experimental</div>] <p>A trait for implementing arbitrary return types in the <code>main</code> function.</p> </td> </tr></table><h2 id='functions' class='section-header'><a href="#functions">Functions</a></h2> <table> <tr class=' module-item'> <td><a class="fn" href="fn.abort.html" title='fn std::process::abort'>abort</a></td> <td class='docblock-short'> <p>Terminates the process in an abnormal fashion.</p> </td> </tr> <tr class=' module-item'> <td><a class="fn" href="fn.exit.html" title='fn std::process::exit'>exit</a></td> <td class='docblock-short'> <p>Terminates the current process with the specified exit code.</p> </td> </tr> <tr class=' module-item'> <td><a class="fn" href="fn.id.html" title='fn std::process::id'>id</a></td> <td class='docblock-short'> <p>Returns the OS-assigned process identifier associated with this process.</p> </td> </tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>⏎</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g. <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g. <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g. <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../../";window.currentCrate = "std";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html>