AdobeStock_455007340

Using CFEXECUTE To Execute Command Line Utilities

A user just wrote to me to ask about using . He was trying to execute a command-line utility (based on an example I posted back in CF 5 days) and the code kept throwing Java errors. His specific problem turned out to be one of pathing, pre CFMX you may not have needed to provide fully qualified paths (for example, the path to cmd.exe) but now you do.
The following is a simple test, it invokes netstat and displays the results:


Executing a shell command (like DIR and SET) is a little trickier. You can’t execute set directly, you’ll need to execute the command interpreter (cmd on Windows) and have it execute set. And you MUST pass /c as an argument – this tells the command interpreter to run and terminate upon completion, without /c it will be waiting for an explicit exit command and will never return.
Here is an example using set:


Note: Obviously these example are Windows specific.

21 responses to “Using CFEXECUTE To Execute Command Line Utilities”

  1. Luis Avatar
    Luis

    Ben,
    I’m trying to use cfexecute to run a command line java class and I’m getting an IO error….do I have to allow for this tag to be run, ie cf7 disable or enable this by default. Do I have to run this under a an account that’s not local?
    Thanks
    -Luis

  2. Alain Avatar
    Alain

    Hi Ben.
    I try to use CFEXECUTE for lunch a format changer for movie (ffmpeg.exe)
    I Make a .bat for run this transforme en flv.
    When i execute the bat normaly (run whis argument) no probleme all is correct. but when i lunch it whis CFEXECUTE, the ffmpeg do not wand ‘stop’ the process, impossible to read the file (file used by a other application).
    I try to use ffmpeg directely in CFEXECUTE whis argument, but shame effect.
    I try different possibility, allway shame effect .. 🙁
    and allway used directely thats work.
    this is realy strange no ?

  3. Danie Avatar
    Danie

    Do you think that it will be possible to call the printer driver from the cfexecute tag and passing the file name that needs to be printed?
    Thanks

  4. Pete Miller Avatar
    Pete Miller

    Hi Ben,
    I’ve looked at your comments above and confirmed the netstat information was returned to the screen.
    I have a batch file d:test.bat which I would like to execute to run on the server.
    My command is as follows
    <cfexecute name="d:test.bat"
    timeout="10">
    </cfexecute>
    This will echo the contents of the file test.bat to the screen rather than execute the file.
    Am I missing something simple

  5. Kivanc Oner Avatar
    Kivanc Oner

    Hi Ben,
    Did you by any chance answer Alain’s question (Posted By Alain | 12/9/06 3:32 AM) before? I think I do have the same problem.
    Actually I do want to download a file within CF from a MVS mainframe secure (SSL) FTP site and I do that by using command line options of a third party FTP client.
    Now, I did write a batch file (batchFile.bat) and when I double click it, it works fine… downloads the file, creates the two log files and etc. However, when I do execute that same batchFile using cfexecute it creates the two log files but hangs on. does not download the file nor writes anything into the log files.
    I wrote a smilar batch file just to test, which creates a directory and a file within, and then deletes those altogether, and it works… it does not hang up…
    Is there a trick to run a third party command line within cfexecute? I am lost…
    your input is very much appreciated….
    Thanks,

  6. Ben Forta Avatar
    Ben Forta

    Kivanc,
    It may be a permissions issue, depending on what your batch file is doing. You may want to try having CF execute under a user account (instead of system service account) to see if it works then.
    — Ben

  7. Sandhya Avatar
    Sandhya

    HI Ben,
    I am trying to run an executable which takes a pdf file as input and returns a tiff file. I am trying to use cfexecute but I am unable to execute that.
    i wrote the executable in a bat file
    Here is the bat file
    pathConvertPDFToImage.exe /S pathInputCoffee.pdf /T pathInputCoffee.tiff /C3
    ur input is appreciated,
    Thanks
    Sandhya

  8. Margaret Avatar
    Margaret

    I’m having this same "hanging" issue trying to execute a Powershell command. Using a batch file it works fine, but running the batch file or the command directly with cfexecute hangs and times out, leaving a dangling Powershell process on the server. I’ve tried the tips at http://cfsearching.blogspot.com/2008/01/cfexecute-ffmpeg-and-mencoder-mystery.html (which you’ve commented on, Ben) and tried running the batch file under the domain credentials that teh CF service runs under, to no avail. This page: http://blogs.sun.com/vaibhav/entry/not_as_easy_as_we talks about problems with the Java Runtime exec() command and Powershell specifically. Maybe cfexecute is having the same problem, but saving the output to a file instead of a variable doesn’t help. So I’m looking for a workaround for Powershell. Anyone know one?

  9. cafe_latte Avatar
    cafe_latte

    I have a related problem with cfexecute in a unix environment.
    It works for a single command under unix.
    But for example, unix commands can be strung together, and cfexecute can’t handle the pipe and subsequent command.
    i.e.: more ./cfseerver.log | grep ‘Coldfusion Starting’
    Is there a work around???

  10. Margaret Avatar
    Margaret

    I’m using a custom-coded Java class that I found on the web and tweaked with a fix that I think is probably PowerShell specific.

  11. anon Avatar
    anon

    For Pete Miller (or anyone else with this problem):
    I was running into the same problem with the batch file showing on the screen as well. You need to add either the variable or outputfile attribute (either are optional) in the command. According to the Adobe docs (as outlined here – http://livedocs.adobe.com/coldfusion/6.1/htmldocs/tags-p25.htm): "If no outputfile or variable attribute is specified, output is displayed on page from which it was called."
    So something like this would work for you:
    <cfexecute name="d:test.bat"
    timeout="10"
    variable="data">
    </cfexecute>
    I added the variable="data" to my cfexecute command and the contents of the batch file went away.

  12. sray Avatar
    sray

    Hello Ben,
    I seem to having a similar problem with cfexecute. In your answer to Kivanc you said that it may be a permission issue. That is the batch is being executed as the system service account. My bat file executes a program and the program hangs if it is executed as the system service account. How do I execute the batch under a user account. Can you point me in some direction to help with this? Perhaps an “execute as” command or something. My batch works fine if it is executed as administrator or other user with the right permissions.
    <cfexecute name="c:windowssystem32cmd.exe"
    arguments="C:filepathmybatch.bat"
    timeout="100"></cfexecute>

  13. donnie snyder Avatar
    donnie snyder

    this describes how to chenge the user account coldfusion runs under
    http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_17279

  14. James Avatar
    James

    I had the same problem with Powershell hanging as Margaret. This seems to be a Powershell issue. I finally found a solution on the Microsoft site. Add "-inputformat none" as one of your parameters.

  15. Nathan Avatar
    Nathan

    Oh my god James Thankyou i have been tearing my hair out trying to work this out… ran cmd as ‘SYSTEM’ (which is the same process CF runs under) and the script worked, but cfexecute didn’t !!!! … This coupled with the fact that for some strange reason cfexecute would always return nothing even when i was deliberately making powershell through an error (outputFile was always empty and variable returned [empty string] ???

  16. Rocky Avatar
    Rocky

    I’ve been having trouble using CFEXECUTE. The argument that is being passed is a .cmd. Its suppose to generate a PDF for a label but instead upon clicking the link that suppose to load the page it gives me 404 (because its not generating the PDF). So naturally I get no page. Is there possibly a permissions issue causing it not to run?
    <— The code —>
    <cffile action="write" file="C:Inetpubwwwrootthe path.xml" output="#shipment#" addnewline="No" nameconflict="overwrite">
    <cfset session.awbno=#shipment.ShipmentValidateResponse.AirwayBillNumber.XmlText#>
    <cfexecute name="C:WINDOWSsystem32cmd.exe" arguments="/C C:Inetpubwwwrootthe path.cmd"> </cfexecute>
    <a href="../path/#session var here#.pdf">Click for Label</a>

  17. Paul Baylis Avatar
    Paul Baylis

    Has anyone got an example of a java runtime version of executing a powershell command, as opposed to the cfexecute version? Apparently it’s less troublesome.

  18. Fabiano Magno Pechibella Avatar
    Fabiano Magno Pechibella

    Hi Ben,
    I need a doubt about how I use the CFEXECUTE to open the SVN and put a document inside in a SVN branch. Can you explain this for me?
    Hugs from Brazil.

  19. Ben Forta Avatar
    Ben Forta

    You should be able to issue any command that you can invoke on the command line. So, open a command prompt window, get your SVN command to work there, and then execute it as described here.
    Also, check riaforge.org, I believe there may be CF SVN integration libraries there.

  20. Joseph Higgins Avatar
    Joseph Higgins

    What happened to all the old content, it is not displaying in the blog. I have referred to the content of this blog many times with different projects. Seems broken now.

    1. BenForta Avatar
      BenForta

      All content was moved to this new blog, but some URLs unfortunately changed so some links broke. But, use Search at the top of the page and you can find all content.

Leave a Reply