EVOLUTION-MANAGER
Edit File: run.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: Run external command, and wait until finishes</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 run {processx}"><tr><td>run {processx}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Run external command, and wait until finishes</h2> <h3>Description</h3> <p><code>run</code> provides an interface similar to <code><a href="../../base/html/system.html">base::system()</a></code> and <code><a href="../../base/html/system2.html">base::system2()</a></code>, but based on the <a href="process.html">process</a> class. This allows some extra features, see below. </p> <h3>Usage</h3> <pre> run( command = NULL, args = character(), error_on_status = TRUE, wd = NULL, echo_cmd = FALSE, echo = FALSE, spinner = FALSE, timeout = Inf, stdout = "|", stderr = "|", stdout_line_callback = NULL, stdout_callback = NULL, stderr_line_callback = NULL, stderr_callback = NULL, stderr_to_stdout = FALSE, env = NULL, windows_verbatim_args = FALSE, windows_hide_window = FALSE, encoding = "", cleanup_tree = FALSE, ... ) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>command</code></td> <td> <p>Character scalar, the command to run. If you are running <code>.bat</code> or <code>.cmd</code> files on Windows, make sure you read the 'Batch files' section in the <a href="process.html">process</a> manual page.</p> </td></tr> <tr valign="top"><td><code>args</code></td> <td> <p>Character vector, arguments to the command.</p> </td></tr> <tr valign="top"><td><code>error_on_status</code></td> <td> <p>Whether to throw an error if the command returns with a non-zero status, or it is interrupted. The error classes are <code>system_command_status_error</code> and <code>system_command_timeout_error</code>, respectively, and both errors have class <code>system_command_error</code> as well. See also "Error conditions" below.</p> </td></tr> <tr valign="top"><td><code>wd</code></td> <td> <p>Working directory of the process. If <code>NULL</code>, the current working directory is used.</p> </td></tr> <tr valign="top"><td><code>echo_cmd</code></td> <td> <p>Whether to print the command to run to the screen.</p> </td></tr> <tr valign="top"><td><code>echo</code></td> <td> <p>Whether to print the standard output and error to the screen. Note that the order of the standard output and error lines are not necessarily correct, as standard output is typically buffered. If the standard output and/or error is redirected to a file or they are ignored, then they also not echoed.</p> </td></tr> <tr valign="top"><td><code>spinner</code></td> <td> <p>Whether to show a reassuring spinner while the process is running.</p> </td></tr> <tr valign="top"><td><code>timeout</code></td> <td> <p>Timeout for the process, in seconds, or as a <code>difftime</code> object. If it is not finished before this, it will be killed.</p> </td></tr> <tr valign="top"><td><code>stdout</code></td> <td> <p>What to do with the standard output. By default it is collected in the result, and you can also use the <code>stdout_line_callback</code> and <code>stdout_callback</code> arguments to pass callbacks for output. If it is the empty string (<code>""</code>), then the child process inherits the standard output stream of the R process. (If the main R process does not have a standard output stream, e.g. in RGui on Windows, then an error is thrown.) If it is <code>NULL</code>, then standard output is discarded. If it is a string other than <code>"|"</code> and <code>""</code>, then it is taken as a file name and the output is redirected to this file.</p> </td></tr> <tr valign="top"><td><code>stderr</code></td> <td> <p>What to do with the standard error. By default it is collected in the result, and you can also use the <code>stderr_line_callback</code> and <code>stderr_callback</code> arguments to pass callbacks for output. If it is the empty string (<code>""</code>), then the child process inherits the standard error stream of the R process. (If the main R process does not have a standard error stream, e.g. in RGui on Windows, then an error is thrown.) If it is <code>NULL</code>, then standard error is discarded. If it is a string other than <code>"|"</code> and <code>""</code>, then it is taken as a file name and the standard error is redirected to this file.</p> </td></tr> <tr valign="top"><td><code>stdout_line_callback</code></td> <td> <p><code>NULL</code>, or a function to call for every line of the standard output. See <code>stdout_callback</code> and also more below.</p> </td></tr> <tr valign="top"><td><code>stdout_callback</code></td> <td> <p><code>NULL</code>, or a function to call for every chunk of the standard output. A chunk can be as small as a single character. At most one of <code>stdout_line_callback</code> and <code>stdout_callback</code> can be non-<code>NULL</code>.</p> </td></tr> <tr valign="top"><td><code>stderr_line_callback</code></td> <td> <p><code>NULL</code>, or a function to call for every line of the standard error. See <code>stderr_callback</code> and also more below.</p> </td></tr> <tr valign="top"><td><code>stderr_callback</code></td> <td> <p><code>NULL</code>, or a function to call for every chunk of the standard error. A chunk can be as small as a single character. At most one of <code>stderr_line_callback</code> and <code>stderr_callback</code> can be non-<code>NULL</code>.</p> </td></tr> <tr valign="top"><td><code>stderr_to_stdout</code></td> <td> <p>Whether to redirect the standard error to the standard output. Specifying <code>TRUE</code> here will keep both in the standard output, correctly interleaved. However, it is not possible to deduce where pieces of the output were coming from. If this is <code>TRUE</code>, the standard error callbacks (if any) are never called.</p> </td></tr> <tr valign="top"><td><code>env</code></td> <td> <p>Environment variables of the child process. If <code>NULL</code>, the parent's environment is inherited. On Windows, many programs cannot function correctly if some environment variables are not set, so we always set <code>HOMEDRIVE</code>, <code>HOMEPATH</code>, <code>LOGONSERVER</code>, <code>PATH</code>, <code>SYSTEMDRIVE</code>, <code>SYSTEMROOT</code>, <code>TEMP</code>, <code>USERDOMAIN</code>, <code>USERNAME</code>, <code>USERPROFILE</code> and <code>WINDIR</code>. To append new environment variables to the ones set in the current process, specify <code>"current"</code> in <code>env</code>, without a name, and the appended ones with names. The appended ones can overwrite the current ones.</p> </td></tr> <tr valign="top"><td><code>windows_verbatim_args</code></td> <td> <p>Whether to omit the escaping of the command and the arguments on windows. Ignored on other platforms.</p> </td></tr> <tr valign="top"><td><code>windows_hide_window</code></td> <td> <p>Whether to hide the window of the application on windows. Ignored on other platforms.</p> </td></tr> <tr valign="top"><td><code>encoding</code></td> <td> <p>The encoding to assume for <code>stdout</code> and <code>stderr</code>. By default the encoding of the current locale is used. Note that <code>processx</code> always reencodes the output of both streams in UTF-8 currently.</p> </td></tr> <tr valign="top"><td><code>cleanup_tree</code></td> <td> <p>Whether to clean up the child process tree after the process has finished.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>Extra arguments are passed to <code>process$new()</code>, see <a href="process.html">process</a>. Note that you cannot pass <code>stout</code> or <code>stderr</code> here, because they are used internally by <code>run()</code>. You can use the <code>stdout_callback</code>, <code>stderr_callback</code>, etc. arguments to manage the standard output and error, or the <a href="process.html">process</a> class directly if you need more flexibility.</p> </td></tr> </table> <h3>Details</h3> <p><code>run</code> supports </p> <ul> <li><p> Specifying a timeout for the command. If the specified time has passed, and the process is still running, it will be killed (with all its child processes). </p> </li> <li><p> Calling a callback function for each line or each chunk of the standard output and/or error. A chunk may contain multiple lines, and can be as short as a single character. </p> </li> <li><p> Cleaning up the subprocess, or the whole process tree, before exiting. </p> </li></ul> <h3>Value</h3> <p>A list with components: </p> <ul> <li><p> status The exit status of the process. If this is <code>NA</code>, then the process was killed and had no exit status. </p> </li> <li><p> stdout The standard output of the command, in a character scalar. </p> </li> <li><p> stderr The standard error of the command, in a character scalar. </p> </li> <li><p> timeout Whether the process was killed because of a timeout. </p> </li></ul> <h3>Callbacks</h3> <p>Some notes about the callback functions. The first argument of a callback function is a character scalar (length 1 character), a single output or error line. The second argument is always the <a href="process.html">process</a> object. You can manipulate this object, for example you can call <code style="white-space: pre;">$kill()</code> on it to terminate it, as a response to a message on the standard output or error. </p> <h3>Error conditions</h3> <p><code>run()</code> throws error condition objects if the process is interrupted, timeouts or fails (if <code>error_on_status</code> is <code>TRUE</code>): </p> <ul> <li><p> On interrupt, a condition with classes <code>system_command_interrupt</code>, <code>interrupt</code>, <code>condition</code> is signalled. This can be caught with <code>tryCatch(..., interrupt = ...)</code>. </p> </li> <li><p> On timeout, a condition with classes <code>system_command_timeout_error</code>, <code>system_command_error</code>, <code>error</code>, <code>condition</code> is thrown. </p> </li> <li><p> On error (if <code>error_on_status</code> is <code>TRUE</code>), an error with classes <code>system_command_status_error</code>, <code>system_command_error</code>, <code>error</code>, <code>condition</code> is thrown. </p> </li></ul> <p>All of these conditions have the fields: </p> <ul> <li> <p><code>message</code>: the error message, </p> </li> <li> <p><code>stderr</code>: the standard error of the process, or the standard output of the process if <code>stderr_to_stdout</code> was <code>TRUE</code>. </p> </li> <li> <p><code>call</code>: the captured call to <code>run()</code>. </p> </li> <li> <p><code>echo</code>: the value of the <code>echo</code> argument. </p> </li> <li> <p><code>stderr_to_stdout</code>: the value of the <code>stderr_to_stdout</code> argument. </p> </li> <li> <p><code>status</code>: the exit status for <code>system_command_status_error</code> errors. </p> </li></ul> <h3>Examples</h3> <pre> # This works on Unix systems run("ls") system.time(run("sleep", "10", timeout = 1, error_on_status = FALSE)) system.time( run( "sh", c("-c", "for i in 1 2 3 4 5; do echo $i; sleep 1; done"), timeout = 2, error_on_status = FALSE ) ) # This works on Windows systems, if the ping command is available run("ping", c("-n", "1", "127.0.0.1")) run("ping", c("-n", "6", "127.0.0.1"), timeout = 1, error_on_status = FALSE) </pre> <hr /><div style="text-align: center;">[Package <em>processx</em> version 3.8.0 <a href="00Index.html">Index</a>]</div> </body></html>