OSSubprocess
OSSubprocess copied to clipboard
How do I redirect stdout/stderr?
The class comment on OSSUnixSubprocess gives an example for output redirection saying
redirectStderrTo: '/tmp/stderrFile.txt' asFileReference writeStream
This no longer works because of the new file stream implementation. Is there another way to get the same effect, or is output redirection unsupported now?
Here is something I discovered and tried:
outputFile := FileLocator temp / 'output.txt'.
self assert: outputFile exists not.
stream := StandardFileStream newFileNamed: outputFile fullName.
stream << 'Hello world!'.
stream close.
self assert: outputFile contents equals: 'Hello world!'
Fine, so let's use this for stdout:
outputFile := FileLocator temp / 'output.txt'.
self assert: outputFile exists not.
stdoutStream := StandardFileStream newFileNamed: outputFile fullName.
self assert: outputFile exists.
OSSUnixSubprocess new
command: '/bin/ls';
arguments: #('/usr');
redirectStdoutTo: stdoutStream;
redirectStderr;
runAndWaitOnExitDo: [ :process |
process isSuccess
ifFalse: [ self error: process exitStatusInterpreter printString ] ].
self assert: outputFile exists
The last assertion fails, meaning that OSSUnixSubprocess deletes the file at some point.
That's not good...
This no longer works because of the new file stream implementation.
Do you already have an idea about the cause then?
The cause for the recommended use pattern not working is pretty simple: '/tmp/stderrFile.txt' asFileReference writeStream returns a stream implemented by a completely different class, which doesn't have the extension method that OSSUnixSubprocess adds to the deprecated stream implementation. The best solution would be to port these extensions to the new stream classes, but I have no idea if that's a simple task or a major effort.
As for my last example deleting the new file, I have no idea so far why this happens.
Ok. Thanks for the help!