1

I am trying to execute this code:

(define dump-db []
 (. (Runtime/getRuntime) exec "pg_dump database-name | gzip -c > db-name.out.gz"))

But the db-name.out.gz file does not is created. Other commands like "ls" or "touch foo.txt" were successfully executed.

Any help?

Édipo Féderle
  • 4,169
  • 5
  • 31
  • 35
  • What happens if you don't pipe to gzip? "pg_dump database-name > db-name.out" ? – S.L. Barth is on codidact.com Jan 26 '15 at 14:15
  • Possible duplicate [(Java) File redirection (both ways) within Runtime.exec?](http://stackoverflow.com/questions/6795924/java-file-redirection-both-ways-within-runtime-exec). – juan.facorro Jan 26 '15 at 14:18
  • Barth, nothing happens. – Édipo Féderle Jan 26 '15 at 14:21
  • 1
    @elf Please check the contents of the output stream from `Process.getOutputStream()` that `Runtime.exec()` returns. The result from the command should be there. AFAICT stdout and stdin redirection don't work when using `ProcessBuilder` or `Runtime.exec()`. – juan.facorro Jan 26 '15 at 14:28
  • 1
    piping and redirecting are "shell features". if in doubt you can "always" use `sh -c 'mycommand | shellfeature > pipetofile'` – cfrick Jan 27 '15 at 00:27

1 Answers1

0

I use this code for now:

(defn foo [line]
(with-open [wrtr (writer "/tmp/test.txt" :append true)]
  (.write wrtr (str line "\n"))))

(->> (.. Runtime getRuntime (exec "pg_dump db-name") getInputStream)
     java.io.InputStreamReader.
     java.io.BufferedReader.
     line-seq
     (map foo))
Édipo Féderle
  • 4,169
  • 5
  • 31
  • 35