3

Suppose I launch a Java application:

java -cp whatever.jar com.example.Start

Process launches ok and keeps running with PID 1314.

Now I would like the system to fire a method by users request.
How can I use bash to signal the running PID and have it fire a method?

Frankie
  • 24,627
  • 10
  • 79
  • 121
  • 1
    Maybe the bash script can write to a file, which you java process can watch/poll. – Scorpion Sep 22 '11 at 17:33
  • 1
    @Scorpion that would work but it's not elegant at all. – Frankie Sep 22 '11 at 18:39
  • yep, you are right but with java 7 watch service it is going to be easier to do and better in terms of performance. I am not sure if a bash script can be used to say send a message to the message queue because that is quite a standard approach to expose functions. – Scorpion Sep 23 '11 at 04:54

2 Answers2

2

My thought is to have bash echo data to the Java processes via a named pipe, which I'm pretty sure Java has support for.

Drizzt321
  • 993
  • 13
  • 27
  • 1
    Java does not, no its own, support Named pipes - it can read / write to them on Linux/Unix via the nio and io packages but it can't make them. – Steven Fines Sep 22 '11 at 18:22
  • Ah, so the launch script would need to create the named pipe, then launch the Java app which would connect to it, and then a later bash script could interact with it via that named pipe. Cool. – Drizzt321 Sep 22 '11 at 19:37
  • Yeah, just mkfifo to make the named pipe... IIRC it'll be persistent so it shouldn't have to be re-made with each script invocation.. but you can use a little bash scripting to see if it exists and make it if not. – Steven Fines Sep 22 '11 at 20:25
0

To communicate with a Java process, you would normally use RMI from another process (this could be in the same JAR)

However, if you want a pure bash/unix utilities solution, you could have the application listen on a port for commands and send back responses. This means you could use plain telnet to send commands and get output. One example of this is to use a http server with wget or you could have a simple socket based solution.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130