In a Clojure program, how do you read from standard out? I want to do that, or pipe the standard output, to an input stream that I create. The standard output in Clojure is a java.io.PrintWriter .
I have a Samza job, started by a Clojure program. There's also an nrepl server to which I can remotely connect. After connecting, I need to be able to tap into and tail standard out (to which jobs write their output).
1) As per this SO question, with-out-str (see here) lets us temporarily bind *out* (to a java.io.StringWriter), so that your executed code writes to a string. But that doesn't let me tap into the existing *out*.
2) If you look at clojure.java.shell (see here), it gets the JVM's Runtime and exec's a Process on it. From that process, you can get its standard output stream. But again, that's not the default standard out (*out*) I'm looking for.
3) This SO question gets close to what I'm trying to do. But again, I'm connecting to an existing process, and want to tail out its standard output.
Is this possible in Clojure (see here)? Has anyone solved this?
Process output is not a publish subscribe model, so in effect when a process puts a character into it's output buffer, exactly one process gets to pull it off that buffer. If you have a program that was started by a shell that shell process if reading it's output and writing it to a terminal (or reading and ignoring it). If you attach your process after the process that started it and start trying to grab the data, you will most likely not get anything because the parent process will get it first. I just tried this from two terminals:
Terminal 1:
cat
Terminal 2:
ps -ef | grep cat
tail -f /proc/24547/fd/2
Terminal 1:
hello
Terminal 2:
< nothing >
The string "hello" printed to terminal 1, the process that started it.
It's tempting then to say "well what if nobody reads the output, then it will be there for me to get". While this sounds good it runs into the problem that these are fixed sized buffers, so as soon as the output buffer is full the process that is trying to write to it blocks (is prevented from running at all) until someone reads the output to unblock it.
The general solution is to pipe the process you want to tail later to the tee command which writes the output to a file and passes it to whatever was reading it.
command-to-watch arg1 arg2 | tee logfile.potentially-huge
Though if you go this route you should rotate the log file before your disk fills. Make sure you empty the log file with exactly this command
echo > logfile.potentially-huge
or use your program to make a truncate call to the file. simply deleting the file will remove it's name from the log directory without deleting it, it will silently continue to grow taking up disk space and the new file will get no output ever.
This is basically why we built log libraries like log4j (in the 90s) and syslog (in the 80s).
If you still want to get hackish crazy on this, turn to tmux, it can do anything, and changes the way people work with text. In all seriousness you should change the way the other process creates it's output to make it easier to get.
Related
i have strange behavior i have csh file that run java process something like this :
run_server.csh
#!/usr/bin/tcsh
java -Dtest=testparam -cp ${TEST}/lib/device.jar:${TEST}/conf:${TEST}/lib/commons-logging-1.1.1.jar com.device.server
when i run it like this :
run_server.csh& in the putty shell in linux
im getting this massage :
[2] + Suspended (tty output) run_server.csh
Although when i run it without the ampersand the server is running and outputting its log massages into the stdout but the problem is when i press ctr+c the process is killed
another strange thing is if i write wrapper script to run_server.csh
that looked like this run_server_wrapper.csh:
#!/usr/bin/tcsh
run_server.csh &
sleep 5
it does run the server as bg process and the run_server_wrapper.csh it self
getting the massage :
[2] + Suspended (tty output) run_server_wrapper.csh
what is the problem here ?
What's happening is that you're putting the process in the background, but it's still writing to the terminal. The terminal driver doesn't approve of that sort of behavior, and suspends the process when it tries to write to the TTY. The obvious answer, of course, is to stop doing that. If you're putting run_server.csh into the background, you should also redirect the output, like so:
run_server.csh > /path/to/serverlog 2>&1 &
If you want the program to run in the background and you still want to see the output, the usual solution is to redirect the output to a log file as I showed, and then monitor the log file with the tail command.
tail -f /path/to/serverlog
If you really, really want to have something running in the background and still able to write to the TTY, turn off the tostop flag.
stty -tostop
One addition to This isn't my real name's useful answer:
If the process you put into the background needs user input, then it will be suspended until you provide input.
I have a CentOS server which is currently running a java jar application. I need to write a php script to communicate with this running program via its input stream. The java program outputs its output to a log file so I don't need access to the output stream.
I don't want to restart the program, just access the running process and interact with it.
Can someone point me in the right direction?
If portability is not a big matter for you, why not creating your own pipe(s)? I don't know much about the java application but have a look at the "mkfifo" function/command.
First, find the ProcessID of the application. You may do it using:
ps -Af | grep java
Since you are using java, you may feel more convenient with the jps command for finding the PID.
I'll assume PID of the running application is 12345. It suffices to issue the command:
cat >/proc/12345/fd/0
And whatever you type in will be put in the standard input of that application. Note that fd contains the file descriptors used by the application, and I suppose the 0-th file descriptor would always be stdin.
Using PHP for writing into the file (and thus being consumed by the application as input) is possible as well.
I have a R script I need to call from Java and run. I tried this code: Runtime.getRuntime().exec("Rscript pathTo/R/myScript.R"). I run it from windows command it worked fine, but when I run java class with this code in Eclipse, nothing happens. The console doesnt show anything no error no logs. Can someone tell me how to run this script from Java?
By default, a Process launched from java has its standard input, standard output and standard error redirected to pipes, which you can access from within java. Unless you read from the standard output and error pipes and transfer the text to the output of the Java application yourself, no output will become visible. Furthermore, if the internal buffer of the pipe gets full, then the child application might even block while waiting for root to write its data. So the process probably will hang and never terminate.
Since Java 7, you can have the child process inherit its I/O channels from your Java application using ProcessBuilder.inheritIO. That saves you all the trouble to read from those streams yourself.
Hi I am trying to execute external program from Java program and read the stdout message in real time, without waiting for the program to exit. However, i found that there are different stdout behaviour in different .exe program, and I don't know how to handle it.
Example 1:
server1.exe is a console program. When i run it, it will continuously listening on a port. When a client is connected to it, it will generate 1 line of stdout output every 1 second. It will not exit unless i press "ctrl-C".
In a command prompt, I run this:
server1.exe > stdout.out 2> stderr.err
When client is connected to it, I found that stdout.out file will be updated in real time. Even though server1.exe is still running, I can open stdout.out file and read the stdout output in real time.
Example 2:
Similar to server1.exe, server2.exe is also a console program. When i run it, it will also continuously listening on a port. When client is connected to it, it will generate 1 line of stdout output every 1 second. It will not exit unless i press "ctrl-C".
In a command prompt, I run this:
server2.exe > stdout.out 2> stderr.err
Even though client has connected to server2.exe, I found that stdout.out file is empty. As long as server2.exe is still running, no stdout is written to stdout.out file. That file is not updated in real time. When i press ctrl-C, it suddenly write many lines of output to stdout.out file.
Assuming that i press ctrl-C at t=11, it will write all stdout output from t=1 until t=11 into the stdout.out file. Before this, at t=10, the stdout.out file is empty.
The program in example 2 is giving me problem because I am unable to read the stdout in real time in my Java program. My java program is as below:
process = Runtime.getRuntime().exec(command);
input = new BufferedReader(new InputStreamReader(process.getInputStream()));
String inputtext = null;
while ((inputtext = input.readLine()) != null)
{
//print out the text in Real Time, when the .exe program is still running
}
May i know why the program in example 2 will not generate stdout output unless I press ctrl-C?
The strange thing is, when i run that program in console window manually, I can see the stdout output printed on the console window every 1 second. But when I try to read it from Java using inputtext = input.readLine(), inputtext will be null as long as the program is still running (I have tested it by printing out inputtext). When I press ctrl-C, the BufferedReader will suddenly be filled with all the pending stdout output.
How can I read stdout of server2.exe in real time?
The way you describe things, there is some buffering happening in your second server. The server might decide to buffer output internally, unless it is connected to a live interactive console window.
While there may be ways to work around this, I would address this in the server2 source code. Whenever that application writes its once-per-second output, it should flush its output streams afterwards. Perhaps there is some option to enable that behaviour. If there isn't, and if the sources of that program are outside your control, kindly ask the developers to add flushing, in order to allow for better integration.
For short: You need to flush the buffers.
System.out.flush()
You need to do this after every chunk of relevant data written on these streams, try doing it after every line print.
I have a batch file which performs the operation of listening to the microphone and converting it to text (i am using pocket sphinx).
The command I am using to run the batch file is pocketsphinx_continuous.exe -dict <dict name> -lm <language model> -hmm <acoustic model location>. The batch file starts off and keeps listening to the microphone. Whenever we finish speaking a sentence it converts it into text on the command prompt. Since this continuously running we terminate this task by Ctrl-C.
I was trying to make this into a standalone Java application. I wanted to run this batch file through Java, so i used Runtime.getRuntime().exec("cmd /c start pocketsphinx_continuous.exe ...") with all the parameters. However strangely, it starts the batch process in a separate command prompt but immediately exits. I tried to use process.waitfor(), however it simply starts executing the batch process and then terminates. I havent called process.destroy, so i am unable to figure out why it exits the batch process.
The other question is since the batch file is running continuously, after every spoken sentence has been transcribed , I wish to get the output in my Java application. I know i can redirect the batch process to a file and then read the file, was just wondering if there is a more direct process. Could you please help me figure out where I am making a mistake.
You should use Process.getInputStream() and Process.getErrorStream() to find out what messages it prints out.
If it is exiting instantly, you might need to get the exit code (Process.waitFor()) and check the error logs from the error stream.
Also, if you can post some of your code we might be able to help. In general, these problems are due to incorrectly configured paths or command strings.
A possible fix would be to use the ProcessBuilder from Java 1.5 thusly:
// Note the lack of "cmd \c" - you shouldn't need to run it in a cmd window!
ProcessBuilder pb = new ProcessBuilder("pocketsphinx_continuous.exe",
"dict", "1121.dic",
"-lm", "1121.lm",
"-hmm", "hub4/hmm");
Process p = pb.start();
// TODO for you:
// 1. Create Threads to handle the input
// 2. Store the Process instance so that you can call p.destroy() later.
// 3. If interested, have a Thread doing p.waitFor() to get the exit code.
As Joachim Sauer mentioned in the comments, this Javaworld article explains a lot of the gotchas associated with the Process API, so have a read through. I haven't had a chance to play with the improvements made in JDK7 to the Process API, and by the looks of things Oracle are improving it again for JDK8 (JEP 102).
You can also use Timer and TimerTask to schedule your batch scan process in background. You can use this to specify infinite running task.