<cfexecute
name=stringarguments=anyoutputFile=stringtimeout=numberterminateOnTimeout=booleanvariable=stringerrorFile=stringerrorVariable=stringdirectory=stringenvironment=structresult=stringexitCodeVariable=stringonProgress=anyonError=any
><!--- body --->[</cfexecute>]
Attribute
Description
name
string, optional
The full pathname of the application to execute.
Note: On Windows, you must specify the extension as part of the application's name. For example,
myapp.exe,
arguments
any, optional
Command-line arguments passed to the application.
arguments as a string
arguments as an Array (recommended)
outputFile
string, optional
The file to which to direct the output of the program. If not specified, the output is
displayed on the page from which it was called.
timeout
number, optional
Indicates how long, in seconds, the executing thread waits for the spawned process.
A timeout of 0 is equivalent to the non-blocking mode of executing. A very high timeout value is
equivalent to a blocking mode of execution. The default is 0; therefore, the thread spawns
a process and returns without waiting for the process to terminate.If no output file is specified,
and the timeout value is 0, the program output is discarded.
terminateOnTimeout
boolean, optional
terminate execution of process when timeout occur. Defaults to false.
variable
string, optional
Variable in which to put program output.
If no output file or variable attribute is specified,
output is displayed on page from which it was called.
errorFile
string, optional
The file to which to direct the error stream of the program. If neither errorFile nor errorVariable is specified, the error stream is discarded.
errorVariable
string, optional
Variable in which to put program error stream output. If neither errorFile nor errorVariable is specified, the error stream is discarded.
directory
string, optional
The working directory in which to execute the command
Introduced: 5.3.8.1
environment
struct, optional
An struct of environment variables
Introduced: 7.0.0.185
result
string, optional
The name of a variable to place the return struct, containing [ output, error, exitCode ]
Introduced: 7.0.0.186
exitCodeVariable
string, optional
Variable in which to put the exit code value.
Introduced: 7.0.0.186
onProgress
any, optional
A UDF which receives two arguments, the progressive output and the java Process instance for the execution.
If defined, output to the other variables will be null.
The java Process instance can be used to interact with the executing process, i.e. via getOutputSteam().
Returning false from the Listener will cancel the process execution
Introduced: 7.0.0.188
onError
any, optional
A UDF Listener which receives a single argument, output.
Optional, If a OnProgress Listener is defined, but no onError listener, the error stream is redirected to the onProgress Listener.
Returning false from the Listener will cancel the process execution
<cfscript>env={"LUCEE":"rocks"};// lucee 7+exe="bash";// args as a stringargs="-c 'set'";// or ls etccfexecute(name=exe,timeout="1",arguments=args,environment=env,variable="result");</cfscript>
Executing a shell command on Windows
<cfscript>env={"LUCEE":"rocks"};// lucee 7+exe="cmd";// args as an arrayargs=["/c","set"];// or dir etccfexecute(name=exe,timeout="1",arguments=args,environment=env,variable="result");</cfscript>
Example for cfexecute with onprogress & onerror (Introduced: 7.0.0.188)
<cfscript>functionhandleProgress(line,process){writeOutput(">> "&line&"<br>");returntrue;// Continue processing}functiononErrorListener(errorOutput){writeOutput("ERROR "&err);returnfalse;//cancel the process};// Execute the "dir" commandcfexecute(name="cmd",//windows command linearguments="/c dir",// "/c" ensures the command runs and exitsresult="local.result",// Captures the output of the command directory="C:\Users",// Specify the directory to listonError=onErrorListener,// Callback function for error handlingonprogress=handleProgress,// Callback function for progresstimeout=10// Set a timeout to prevent hanging);</cfscript>