Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Clojure, how do I send an exit command to a process created by Clojure.java.shell/sh

I am trying to run pocketsphinx from clojure. I have written the following .sh script

unbuffer pocketsphinx_continuous -innmic yes > pipe and I want to call this process using (shell/sh).

Unbuffer allows pocketsphinx to properly flush its data out to pipe where it can be read line by line.

The problem I am having is I do not know how to properly kill the process. It will run forever and never return control. If I kill the thread it is running on, the process that the sh command spawned still runs. The only thing I can think of is running kill on the pid

like image 585
Collin Bell Avatar asked Sep 09 '16 21:09

Collin Bell


People also ask

How do I exit Clojure?

Using the CLI tools See Getting Started to learn how to install the Clojure CLI. You can exit the REPL by typing Ctrl+D (pressing the Ctrl and D keys at the same time).

How do you abruptly stop execution in the REPL?

You can just press ctrl-d (*nix) or ctrl-z (Windows) to exit the REPL.

What is clojure Main?

The clojure. main namespace provides functions that allow Clojure programs and interactive sessions to be launched via Java's application launcher tool java .


1 Answers

clojure.java.shell/sh won't return until the process being executed completes and you would need to use another thread to use kill on a pid.

Another solution would be to use java.lang.Runtime.exec or java.lang.ProcessBuilderto get a java.lang.Process instance which provides methods like destroy or destroyForcibly.

You can also use a Raynes/conch library if you still would like to have a nice Clojure API and still be able to run a command and specify a timeout for the process to complete and still get the output produces until the timeout occured.

like image 174
Piotrek Bzdyl Avatar answered Oct 30 '22 04:10

Piotrek Bzdyl