How to execute system commands?

后端 未结 3 851
攒了一身酷
攒了一身酷 2021-02-01 12:26

How can I execute system specific commands and get their response in Clojure? For example, let\'s assume we\'re on a Linux machine, how can I call top or free

相关标签:
3条回答
  • 2021-02-01 13:04

    If you are willing to get a little higher in term of abstractions (although no that high), I would recommend Conch, as I found it to make very readable code.

    0 讨论(0)
  • 2021-02-01 13:16
    (use '[clojure.java.shell :only [sh]])
    (sh "free")
    (sh "top" "-bn1")
    

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

    0 讨论(0)
  • 2021-02-01 13:21

    You should be able to 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 you can query to get the standard output etc. as needed.

    0 讨论(0)
提交回复
热议问题