1

My attempt:

(import 'java.lang.Runtime)
(. (Runtime/getRuntime) exec (into-array ["youtube-dl" "--no-playlist" "some youtube video link"]))

I also tried sh. But both approaches don't do what I want - running a program similarly like shell does (sh waits until program exits, exec launches it and doesn't wait for its exit; both don't output anything to standard output). I want live showing of process output, e.g. when I run youtube-dl I want to see progress of a video download.

How to do this simple simple task in Clojure?

1 Answer 1

2

You must start the process and listen to its output stream. One solution is :

(:require [clojure.java.shell :as sh]
          [clojure.java.io :as io])

(let [cmd ["yes" "1"]
      proc (.exec (Runtime/getRuntime) (into-array cmd))]
      (with-open [rdr (io/reader (.getInputStream proc))]
        (doseq [line (line-seq rdr)]
          (println line))))
Sign up to request clarification or add additional context in comments.

2 Comments

It's not exactly same as shell does it (e.g. shell allows to "rewrite" current line, so for example progress bar is always shown only once), but it's close enough. Thank you :).
In my example I just use the println function. For the rewrite behaviour you must implement your own println function that prints the carriage return. See: stackoverflow.com/questions/9566654/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.