简体   繁体   中英

clojure — how to run a program without piping it's output?

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 (eg in the case that the subprocess may fail).

edit

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

Also see this third party library

https://github.com/Raynes/conch

It provides direct access to the streams.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM