"Inject" commands into a running process
I've made a Perl script to start a Java game server, java -jar somejar.jar > /dev/null 2>&1 &
It starts and runs normally, but I would like to be able to 'inject' commands into the running server console (to stop restart etc) because it is dangerous to stop by terminating it via killall or ^C.
When run normally the server displays a log of user activities and an area to type commands in, that is where I would like t开发者_C百科o 'inject' the text.
Is this possible?
Thanks! Justin
It won't be reasonable to do this without tweaking the Java application. Change your application such that it can read commands from both the input console but also from a socket. Create a server socket listening for incoming connections, read the data and interpret it as a command. From the Perl script or wherever you want to execute the command, connect to the server socket's listening port and write the command string to the socket.
You can even create a clean abstraction that will be agnostic to the "command source".
- Java Sockets Tutorial
Note that there is a restricted API that allows you to trap and interpret POSIX signals but it's hidden and should probably not be used.
As discussed in this question, binding stdin to a FIFO won't work, because your server process would see a separate EOF after each time you pass new commands into the FIFO. I'm guessing the simplest solution would be to run the server in Screen. The nicer, but more technical solution would be to modify the server to accept commands through for example a socket.
精彩评论