CFEXECUTE on Unix

Usually, I don’t have many issues that are unix-specific when working in ColdFusion. CFEXECUTE is one of those tags that is a little more, shall we say… annoying? It’s quite common to try and execute a command line in Unix and have no result and no errors.

Here’s some tips that I’ve found helpful throughout the years:

  1. Try specifying the full path. Don’t just “tar” “ln” or “echo” — specify the full path, like “/usr/bin/ln” instead.
  2. You do need to make sure that you have permissions to execute that command. Try it manually as your CF user.
  3. Arguments can be tricky. The CF docs say, if passed as a string, it needs to be “tokenized into an array of arguments” and “The default token separator is a space; you can delimit arguments that have embedded spaces with double-quotation marks.” If passed as an array, “elements are copied into an array of exec() arguments”. If it’s a simple argument, using a string is fine. For something with multiple arguments, I’ve found that it’s just cleaner to use the array convention.
  4. Always specify a timeout value.

 

Here’s a few examples…

Creating a symlink:

<cfexecute name="/usr/bin/ln" arguments="-s ""#myDir##myFilename#"" #symlinkDir#/#symlinkFilename#" timeout="5" />

Creating a zipped tarball (data.tar.gz):

In this example, I’m creating an array of arguments for both cfexecute tags, just because it looked cleaner.

<cfset tarArgsĀ  = [ "-cf", "#myDirectory#/data.tar", "-C", "#directoryOfFilesToGet#", "." ] >
<cfset gzipArgs = [ "#myDirectory#/data.tar" ] >
<cfexecute name="/usr/bin/tar" arguments="#tarArgs#" timeout="60" />
<cfexecute name="/usr/bin/gzip" arguments="#gzipArgs#" timeout="60" />

Leave a Reply

Your email address will not be published. Required fields are marked *