VMware Cloud Community
schistad
Enthusiast
Enthusiast
Jump to solution

Stderr in external command execution seems to get lost

After some initial puzzlement I've discovered that when you run external programs, only file stream 1 (STDOUT) is captured in the command.output method. Unfortunately, there is no error handle and redirection does not seem to work either (eg command -parm arg1 2>&1).

Do I need to write bash wrappers to redirect stderr, or is there a way to retrieve it from the result object?

Reply
0 Kudos
1 Solution

Accepted Solutions
schistad
Enthusiast
Enthusiast
Jump to solution

This is on linux (the vCO Appliance), and I ended up doing pretty much this; I wrote a wrapper which invokes bash and runs $@ 2>&1.

Eg:

#!/bin/bash

CMD=$1

shift

ARG=$@

$CMD $ARG 2>&1

View solution in original post

Reply
0 Kudos
5 Replies
cdecanini_
VMware Employee
VMware Employee
Jump to solution

This should work:

scriptArguments = "/s /c cmd >" + scriptOutputFile + " 2>&1 /s /c " + script;

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vCenter Orchestrator tips and tutorials - @vCOTeam on Twitter
Reply
0 Kudos
igaydajiev
VMware Employee
VMware Employee
Jump to solution

In case you are running cmd.exe you can do someting like this :

cmd = new Command("cmd.exe /c dir NON_EXISTING_FOLDER 2>&1");

cmd.execute(true);

System.log(cmd.output)

Reply
0 Kudos
schistad
Enthusiast
Enthusiast
Jump to solution

This is on linux (the vCO Appliance), and I ended up doing pretty much this; I wrote a wrapper which invokes bash and runs $@ 2>&1.

Eg:

#!/bin/bash

CMD=$1

shift

ARG=$@

$CMD $ARG 2>&1

Reply
0 Kudos
cdecanini_
VMware Employee
VMware Employee
Jump to solution

Sorry my answer was for running a Windows command in a guest using vCenter guest operations workflows.

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vCenter Orchestrator tips and tutorials - @vCOTeam on Twitter
Reply
0 Kudos
vbranden
Contributor
Contributor
Jump to solution

I got around this by writing the command text to a temp file and executing it with stdout/stderr redirection to temp files for stdout and stderr then cleaning up the files at the end. Here is my action that takes a single parameter "command". this is working on embedded vRO in vRA 7.5

// script file

var scriptFile = System.createTempFile(".sh")

scriptFile.createFile()

var scriptFilePath = scriptFile.path

// stderr file

var stdErrFile = System.createTempFile()

stdErrFile.createFile()

var stdErrFilePath = stdErrFile.path

// stdout file

var stdOutFile = System.createTempFile()

stdOutFile.createFile()

var stdOutFilePath = stdOutFile.path

// build and execute command by creating and executing a script

var scriptText = "#!/bin/bash\n\n" + command + " 2> " + stdErrFilePath + " 1> " + stdOutFilePath

var scriptFr = new FileWriter(scriptFilePath)

scriptFr.open()

scriptFr.write(scriptText)

scriptFr.close()

var chmodCmd = new Command("chmod +x " + scriptFilePath)

chmodCmd.execute(true)

var cmd = new Command(scriptFilePath)

cmd.execute(true)

// get the output

var result = {

  exitCode: cmd.result

}

scriptFile.deleteFile()

var errFr = new FileReader(stdErrFilePath)

errFr.open()

result.stderr = errFr.readAll().trim().replace(/\r/g, "")

stdErrFile.deleteFile()

var outFr = new FileReader(stdOutFilePath)

outFr.open()

result.stdout = outFr.readAll().trim().replace(/\r/g, "")

stdOutFile.deleteFile()

return result

Reply
0 Kudos