noahnu
noahnu

Reputation: 3574

How do I write to the input stream of an already running java program?

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?

Upvotes: 4

Views: 534

Answers (2)

morandg
morandg

Reputation: 1066

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.

Upvotes: 1

semekh
semekh

Reputation: 3917

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.

Upvotes: 1

Related Questions