Quantcast
Viewing all articles
Browse latest Browse all 49146

Re: Stderr in external command execution seems to get lost

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

Viewing all articles
Browse latest Browse all 49146

Trending Articles