EVOLUTION-MANAGER
Edit File: exec.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Running System Commands</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for exec {sys}"><tr><td>exec {sys}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Running System Commands</h2> <h3>Description</h3> <p>Powerful replacements for <a href="../../base/html/system2.html">system2</a> with support for interruptions, background tasks and fine grained control over <code>STDOUT</code> / <code>STDERR</code> binary or text streams. </p> <h3>Usage</h3> <pre> exec_wait( cmd, args = NULL, std_out = stdout(), std_err = stderr(), std_in = NULL, timeout = 0 ) exec_background( cmd, args = NULL, std_out = TRUE, std_err = TRUE, std_in = NULL ) exec_internal(cmd, args = NULL, std_in = NULL, error = TRUE, timeout = 0) exec_status(pid, wait = TRUE) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>cmd</code></td> <td> <p>the command to run. Either a full path or the name of a program on the <code>PATH</code>. On Windows this is automatically converted to a short path using <a href="../../base/html/Sys.which.html">Sys.which</a>, unless wrapped in <code><a href="../../base/html/AsIs.html">I()</a></code>.</p> </td></tr> <tr valign="top"><td><code>args</code></td> <td> <p>character vector of arguments to pass. On Windows these automatically get quoted using <a href="quote.html">windows_quote</a>, unless the value is wrapped in <code><a href="../../base/html/AsIs.html">I()</a></code>.</p> </td></tr> <tr valign="top"><td><code>std_out</code></td> <td> <p>if and where to direct child process <code>STDOUT</code>. Must be one of <code>TRUE</code>, <code>FALSE</code>, filename, connection object or callback function. See section on <em>Output Streams</em> below for details.</p> </td></tr> <tr valign="top"><td><code>std_err</code></td> <td> <p>if and where to direct child process <code>STDERR</code>. Must be one of <code>TRUE</code>, <code>FALSE</code>, filename, connection object or callback function. See section on <em>Output Streams</em> below for details.</p> </td></tr> <tr valign="top"><td><code>std_in</code></td> <td> <p>file path to map std_in</p> </td></tr> <tr valign="top"><td><code>timeout</code></td> <td> <p>maximum time in seconds</p> </td></tr> <tr valign="top"><td><code>error</code></td> <td> <p>automatically raise an error if the exit status is non-zero.</p> </td></tr> <tr valign="top"><td><code>pid</code></td> <td> <p>integer with a process ID</p> </td></tr> <tr valign="top"><td><code>wait</code></td> <td> <p>block until the process completes</p> </td></tr> </table> <h3>Details</h3> <p>Each value within the <code>args</code> vector will automatically be quoted when needed; you should not quote arguments yourself. Doing so anyway could lead to the value being quoted twice on some platforms. </p> <p>The <code>exec_wait</code> function runs a system command and waits for the child process to exit. When the child process completes normally (either success or error) it returns with the program exit code. Otherwise (if the child process gets aborted) R raises an error. The R user can interrupt the program by sending SIGINT (press ESC or CTRL+C) in which case the child process tree is properly terminated. Output streams <code>STDOUT</code> and <code>STDERR</code> are piped back to the parent process and can be sent to a connection or callback function. See the section on <em>Output Streams</em> below for details. </p> <p>The <code>exec_background</code> function starts the program and immediately returns the PID of the child process. This is useful for running a server daemon or background process. Because this is non-blocking, <code>std_out</code> and <code>std_out</code> can only be <code>TRUE</code>/<code>FALSE</code> or a file path. The state of the process can be checked with <code>exec_status</code> which returns the exit status, or <code>NA</code> if the process is still running. If <code>wait = TRUE</code> then <code>exec_status</code> blocks until the process completes (but can be interrupted). The child can be killed with <a href="../../tools/html/pskill.html">tools::pskill</a>. </p> <p>The <code>exec_internal</code> function is a convenience wrapper around <code>exec_wait</code> which automatically captures output streams and raises an error if execution fails. Upon success it returns a list with status code, and raw vectors containing stdout and stderr data (use <a href="as_text.html">as_text</a> for converting to text). </p> <h3>Value</h3> <p><code>exec_background</code> returns a pid. <code>exec_wait</code> returns an exit code. <code>exec_internal</code> returns a list with exit code, stdout and stderr strings. </p> <h3>Output Streams</h3> <p>The <code>std_out</code> and <code>std_err</code> parameters are used to control how output streams of the child are processed. Possible values for both foreground and background processes are: </p> <ul> <li> <p><code>TRUE</code>: print child output in R console </p> </li> <li> <p><code>FALSE</code>: suppress output stream </p> </li> <li> <p><em>string</em>: name or path of file to redirect output </p> </li></ul> <p>In addition the <code>exec_wait</code> function also supports the following <code>std_out</code> and <code>std_err</code> types: </p> <ul> <li> <p><em>connection</em> a writable R <a href="../../base/html/connections.html">connection</a> object such as <a href="../../base/html/showConnections.html">stdout</a> or <a href="../../base/html/showConnections.html">stderr</a> </p> </li> <li> <p><em>function</em>: callback function with one argument accepting a raw vector (use <a href="as_text.html">as_text</a> to convert to text). </p> </li></ul> <p>When using <code>exec_background</code> with <code>std_out = TRUE</code> or <code>std_err = TRUE</code> on Windows, separate threads are used to print output. This works in RStudio and RTerm but not in RGui because the latter has a custom I/O mechanism. Directing output to a file is usually the safest option. </p> <h3>See Also</h3> <p>Base <a href="../../base/html/system2.html">system2</a> and <a href="../../base/html/connections.html">pipe</a> provide other methods for running a system command with output. </p> <p>Other sys: <code><a href="exec_r.html">exec_r</a></code> </p> <h3>Examples</h3> <pre> # Run a command (interrupt with CTRL+C) status <- exec_wait("date") # Capture std/out out <- exec_internal("date") print(out$status) cat(as_text(out$stdout)) if(nchar(Sys.which("ping"))){ # Run a background process (daemon) pid <- exec_background("ping", "localhost") # Kill it after a while Sys.sleep(2) tools::pskill(pid) # Cleans up the zombie proc exec_status(pid) rm(pid) } </pre> <hr /><div style="text-align: center;">[Package <em>sys</em> version 3.4 <a href="00Index.html">Index</a>]</div> </body></html>