View comments | RSS feed

cfexecute

Description

Executes a ColdFusion developer-specified process on a server computer.

Category

Extensibility tags, Flow-control tags

Syntax

<cfexecute 
name = "application name"
arguments = "command line arguments"
outputFile = "output filename" variable = "variable name"
timeout = "timeout interval">
...
</cfexecute>

See also

cfcollection, cfindex, cfobject, cfreport, cfsearch, cfwddx

History

ColdFusion MX 6.1:

Attributes

Attribute Req/Opt Default Description

name

Required

 

Absolute path of the application to execute.

On Windows, you must specify an extension; for example, C:\myapp.exe.

arguments

Optional

 

Command-line variables passed to application. If specified as string, it is processed as follows:

  • Windows: passed to process control subsystem for parsing.
  • UNIX: tokenized into an array of arguments. The default token separator is a space; you can delimit arguments that have embedded spaces with double-quotation marks.

If passed as array, it is processed as follows:

  • Windows: Elements are concatenated into a string of tokens, separated by spaces. Passed to process control subsystem for parsing.
  • UNIX: Elements are copied into an array of exec() arguments.

outputFile

Optional

 

File to which to direct program output. If no outputfile or variable attribute is specified, output is displayed on the page from which it was called.

If not an absolute path (starting a with a drive letter and a colon, or a forward or backward slash), it is relative to the ColdFusion temporary directory, which is returned by the GetTempDirectory function.

variable

Optional

 

Variable in which to put program output. If no outputfile or variable attribute is specified, output is displayed on page from which it was called.

timeout

Optional

0

Length of time, in seconds, that ColdFusion waits for output from the spawned program.

  • 0: equivalent to nonblocking mode.
  • A very high value: equivalent to blocking mode.

If the value is 0:

  • ColdFusion starts a process and returns immediately. ColdFusion may return control to the calling page before any program output displays. To ensure that program output displays, set the value to 2 or higher.
  • If the outputFile attribute is not specified, any program output is discarded

Usage

Do not put other ColdFusion tags or functions between the start and end tags of cfexecute. You cannot nest cfexecute tags.

Exceptions

Throws the following exceptions:

The time out values must be between zero and the longest time out value supported by the operating system.

Example

<h3>cfexecute</h3>
<p>This example executes the Windows NT version of the netstat network 
monitoring program, and places its output in a file. <cfexecute name = "C:\WinNT\System32\netstat.exe" arguments = "-e" outputFile = "C:\Temp\output.txt" timeout = "1"> </cfexecute>

ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | KnowledgeBase | Bug Reporting

Version 7

Comments


cf_Amir said on Sep 28, 2005 at 10:21 AM :
Read this and save yourself hours.

If you don't specify a TIMEOUT, the execution is asynchronous. A new thread is spawned to do the work, and there is no output written to the screen nor the variable when outputfile are not provided.

So, if you want output on the screen or variable make sure you provide the TIMEOUT attribute.
the_falling_frog said on Aug 31, 2006 at 1:13 PM :
Read this and save yourself more hours.

Windows NT Error 21 means File Not Found.
bizarrojack said on Sep 28, 2006 at 11:41 PM :
Conspicuously absent is a way to send any data to the program on its standard input. (Less useful, but also absent, there is no way to distinguish between standard output and standard error). If there is any more complex instructions for your program than what can come in as a "command line" style argument, you'll have to do something complicated like put the program input into a file (if your external app supports that), and specify that (temp) file's name in the arguments. Don't forget to clean up the temp file and make sure that it doesn't make a mess if the template is running more than once concurrently. Catch exceptions so your delete operation on the temp file doesn't have any chance of escaping.

A better alternative, when you're dependent on one external program that would benefit from use of standard input, may be to write a wrapper program in perl or python, or just avoid coldfusion entirely.

Depending on your OS and the available shells, there is probably some tricky way of calling your program via a shell and redirecting arguments into stdin, if you can handle all the escaped quotation marks.
bizarrojack said on Sep 28, 2006 at 11:59 PM :
I should add that if you try to run a shell to call your other external program, and the input you're trying to redirect is in any way based on user input, it's a pretty ridiculous security risk and you shouldn't do it. There are a lot of ways to escape and unescape characters that have special meaning to the shell and could end up causing all sorts of problems.
carehart said on Jan 17, 2007 at 10:16 AM :
Folks, the table listing the discussion of the TimeOut attribute needs to be updated as others above it were, to add reference to the new Variable attribute. Where it says:

"If the outputFile attribute is not specified, any program output is discarded "

it should say:

"If the outputFile or Variable attribute is not specified, any program output is discarded "
Steve Seaquist said on Feb 3, 2007 at 10:55 AM :
Read this and regain the full power of the Unix command line!

I read complaints about stuff you can't do, like stderr redirection, etc. Did
you forget about cfexecuting the shell itself? I don't know about all shells,
because I only have man pages for bash and tcsh as I type this, but bash
and tcsh both take a -c "command line" argument. Within those quotes,
you can do pipes and tees and redirections and greps and whatever
else you like, including multiple commands using semicolons. Use bash
if you want to redirect stderr, because tcsh doesn't have a syntax for that.
Also within those quotes, if a command isn't a built-in shell command,
such as cd, I give its full path name. Not sure if I have to, but I do.

Under Solaris at work, I like to compare directories by passing -c "cd /opt/
iplanet/servers/docs ; diff dirname1 dirname2 | grep diff | grep -vi datediff
| grep -vi differen ; cd -". That gives me a concise list of all files that are
different in "diff dirname1/filename dirname2/filename" format. I retrieve
that into a ColdFusion variable and parse it out. Because what I'm
comparing are cfm files, note that I exclude datediff from the list, in case it
appears in the differences listing under the diff. Same for differences and
different in comments and background text. Your excludable garbage
may vary.

Now go forth and wow your users with all the cool stuff you can do in
Unix!
Steve Seaquist said on Feb 3, 2007 at 11:02 AM :
In my previous comment about cfexecute, I neglected to say /bin/diff and /
bin/grep in the example. Please edit in /bin/ before the diff and 3 greps, but
not the cds. Thanks.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/7/htmldocs/00000243.htm