One Programinstance

Hello World!
I’m trying to write a server application, for which I have a few Questions. My problem is, that I only want to have one active Serverinstance running at anytime. Because I only want to use the typical console on windows or the terminal on linux to run my server, I have the problem, that I can’t get my running server instance back, when I eventually close my Terminal/console. My Question is now, that I need a way to at least contact my earlier startet Serverinstance, even if I closed its Terminal, can someone show me a way to do that? My Idea is creating ServerSocket in my running Serverinstance, so that I have a way to contact it, but because it should only accept commands from localhost I don’t know how to write it the right way, so I would like, if I there is a way without a ServerSocket out there.
Another problem is the part, where I check for another Serverinstance has anyone an idea for that?

Greetings
biro

The key is to run your server as a daemon. You can either do this when invoking your program or you can create a thread and use Thread.setDaemon(true).

:cranky: Please read the javadoc before you ‘help’ others.

Whoops, I stand corrected. setDaemon(false).

That’s the default value, so it doesn’t really change anything.

The point is that Daemon Threads are in no way related to Daemon Processes.

What you (probably) want is:
http://www.cyberciti.biz/tips/how-to-use-screen-command-under-linux.html

It gives you a virtual terminal that you can open/close (attach/detach) while keeping the process running.

Hello,
thank you for your fast answer, but I’m not sure if that’s what I need…
I’m searching for a way to write a program that can be operated the same way as mysql on linux, as example.
When you start mysql on linuy you need the command: mysql start
One command -> One operation and it doesn’t block the terminal.
If I want to use another command on my server like getting its status I write: mysql status
One command -> One Operation

I want to implement this behavior in my server. I don’t want to have a server app that blocks my terminal full time. And I don’t want an application that I can’t operate when I close its terminal.

Greetings
biro

You’re probably gonna have to use some shady Runtime.exec implementation then which is bad because debugging becomes a pain and screen just werks.

Take a look at the Java Service Wrapper, which does exactly what you’re looking for.