6

I want to use something like shell-out [ http://richhickey.github.com/clojure-contrib/shell-out-api.html ], but without capturing the any output. Of course the output can be passed to print, but this is slightly less than desirable (e.g. in the case that the subprocess may fail).

edit

Sorry, I want the subprocess to output to the same stdout as the parent process.

2
  • 1
    Do you want to silence the subprocess ( > /dev/null ) or do you want the subprocess to use the same stdout as the parent process? In any case, you have the full range of JDK classes at your disposal.
    – Bendlas
    Aug 11, 2011 at 13:31
  • the latter; the question has been edited. Aug 11, 2011 at 20:11

2 Answers 2

5

Also see this third party library

https://github.com/Raynes/conch

It provides direct access to the streams.

3

EDIT: Before Clarification

You can wrap the shell command with a sh and then pipe to /dev/null like so:

 (clojure.java.shell/sh "sh" "-c" "echo hello > /dev/null")
 ;; {:exit 0, :out "", :err ""}

This will silence the output before getting to clojure.

EDIT: After Clarification

Passing output and stderr to print should work as long as the output comes out quickly enough. If you want something with continuous output of error messages and standard output, looking at the source for the "sh" function should help.

Personally, I would make my own version of clojure.java.shell/sh and for each stream, create a thread that pipes the output directly to out using something like IOUtils.copy from org.apache.commons.io.IOUtilsin

3
  • 1
    Invoking the system shell is totally non-portable. Also, it doesn't seem this can be done using Process.exec(). I guess the portable version will have to wait until Java 7's ProcessBuilder is released. Aug 11, 2011 at 20:18
  • 1
    if you don't read the output promptly enough the process could block on writes to its stdout and appear to hang. same for stderr Aug 12, 2011 at 18:23
  • @Arthur, yes, and also if everything gets killed the print might not execute, and you might miss valuable debugging information from the partial output of the subprocess. Aug 12, 2011 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.