How to execute system commands?

How can I execute system-specific commands and get their response in Clojure? For example, suppose we are on a Linux machine, how can I call top or free and get their results for further processing?

+47
clojure
Jul 18 2018-11-18T00:
source share
3 answers
 (use '[clojure.java.shell :only [sh]]) (sh "free") (sh "top" "-bn1") 

See also: http://clojuredocs.org/clojure_core/clojure.java.shell/sh

+68
Jul 18 '11 at 17:12
source share

You can use the Java Runtime.exec method as follows:

 (import 'java.lang.Runtime) (. (Runtime/getRuntime) exec "your-command-line-here") 

The Runtime.exec method returns a Process object that can be requested to receive standard output, etc. as needed.

+11
Jul 18 '11 at 16:21
source share

If you want to get a little more in terms of abstractions (although not so high), I would recommend Conch , since I found that it makes very readable code.

+1
Sep 10 '15 at 12:31
source share



All Articles