2

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 to 'inject' the text.

Is this possible?

Thanks! Justin

Justin
  • 808
  • 1
  • 7
  • 15
  • So the application only exposes an interactive interface? Could you elaborate on that? No batch mode or command line functionality? Are you using Linux or Windows? – kmarks2 May 09 '11 at 15:50
  • This is running on CentOS 5.5 64bit with the official Sun/Oracle JVM, the console contains an upper part with the game log and contains a line on the bottom to input commands. The input line does not seem special because as new items appear in the game log your command scrolls with it, even though it scrolls with the log, the full command is executed after pressing enter. – Justin May 09 '11 at 16:10

2 Answers2

3

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".

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.

Mark Peters
  • 80,126
  • 17
  • 159
  • 190
0

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.

Community
  • 1
  • 1
gspr
  • 11,144
  • 3
  • 41
  • 74