GUI-less application

  • Thread starter Martin Stokland Jensen
  • Start date
M

Martin Stokland Jensen

Hi

Here is my problem. I'm currently modifying a program to run without
the GUI. It shall run on a headless system where X is not installed.
When I remove the statement that invokes the GUI, the program just
continues reading statements and exits. The GUI somehow kept the
application running.

I can make a infinite loop here like this:

boolean ever = true;
for (;ever;) {}

Now everything works. The program runs just fine the way I want it.
But the problem is that the only way (I know of) to terminate it, is
to kill it. I would like til to just stop wait()ing and execute the
last statements in the code.

How can I send a signal to the program? Can a seperate program do this?

I don't think any source code should be nessesary since this is a general
question, but the code looks like this:

<code>
public static void main(String[] args) {
MainWindow.instance().open();
cleanUpMethod();
System.exit(0);
}
</code>

I changed it to

<code>
public static void main(String[] args) {
boolean ever = true;
for (;ever;) {}
cleanUpMethod();
System.exit(0);
}
</code>

But cleanUpMethod() and System.exit(0) will never run, 'cause I kill
it to stop it.


Thanks
Martin Jensen
 
G

GaryM

But cleanUpMethod() and System.exit(0) will never run, 'cause I
kill it to stop it.

Try:

Runtime.getRuntime().addShutdownHook(
new Thread(new Runnable() {

public void run() {
System.out.println("Shutdown requested.");
cleanUpMethod();
System.out.println("System exiting by request";
}
}));
 
O

Oscar kind

Martin Stokland Jensen said:
Here is my problem. I'm currently modifying a program to run without
the GUI. It shall run on a headless system where X is not installed.
When I remove the statement that invokes the GUI, the program just
continues reading statements and exits. The GUI somehow kept the
application running.

That's because when you open a window, an event handler thread is started.
You removed the window, so the thread isn't started. The only thread is
the thread for the main method, which calls System.exit(int) to close the
JVM.

I can make a infinite loop here like this:

boolean ever = true;
for (;ever;) {}

Now everything works. The program runs just fine the way I want it.

Rally? That means that you start a new thread yourself that does the
actual work. But if you start a thread for that, why not do the work in
main(String[])? Then main(String[]) doesn't have to wait.

But the problem is that the only way (I know of) to terminate it, is
to kill it. I would like til to just stop wait()ing and execute the
last statements in the code.

Use the boolean variable you defined above, but change it into a member
variable. Let (one of) the extra thread(s) you start set it. Then the loop
will terminate.
 
Z

zoopy

Try:

Runtime.getRuntime().addShutdownHook(
new Thread(new Runnable() {

public void run() {
System.out.println("Shutdown requested.");
cleanUpMethod();
System.out.println("System exiting by request";
}
}));

I don't know how OP "kills" his program, but there are situations where a shutdown hook doesn't get
executed.

Shutdown hooks will run upon a normal (clean) shutdown of the VM, i.e. when:

<quote from addShutdownHook API doc>
- The program exits normally, when the last non-daemon thread exits or when the exit (equivalently,
System.exit) method is invoked, or
- The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a
system-wide event, such as user logoff or system shutdown.
</quote>


But it's not guaranteed that a shutdown hook will run when:

<quote from same doc>
In rare circumstances the virtual machine [aborts], that is, [stops] running without shutting down
cleanly. This occurs when the virtual machine is terminated externally, for example with the SIGKILL
signal on Unix or the TerminateProcess call on Microsoft Windows.
</quote>
 
M

Martin Stokland Jensen

I don't know how OP "kills" his program, but there are situations where a shutdown hook doesn't get
executed.

I would kill it with kill -9 as I don't know any other way. The shutdown
hook may be an idea, but it only seems to work with normal
shutdown-procedures.

How does programs normally run idly in the background without GUI's in
Java. I want to be able to have it running, and write

myApp stop

to have it gracefully shut down.

Shutdown hooks will run upon a normal (clean) shutdown of the VM, i.e. when:

<quote from addShutdownHook API doc>
- The program exits normally, when the last non-daemon thread exits or when the exit (equivalently,
System.exit) method is invoked, or
- The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a
system-wide event, such as user logoff or system shutdown.
</quote>

Then I don't think the shutdownhook will help.


Martin Jensen
 
G

GaryM

I don't know how OP "kills" his program, but there are situations
where a shutdown hook doesn't get executed.

Agreed, but kill -SIGTERM does work and this is a nice way to shut the
process down, which is what OP wanted I thought.

Is there a better way?
 
G

GaryM

I would kill it with kill -9 as I don't know any other way. The
shutdown hook may be an idea, but it only seems to work with
normal shutdown-procedures.

Why do you have to use -9? If you just kill it, it will send TERM
signal which will invoke the shutdown hook I mentioned?
How does programs normally run idly in the background without
GUI's in Java. I want to be able to have it running, and write

myApp stop

to have it gracefully shut down.

If myApp is java program then it could be that on receiving the
parameter 'stop' it connects to a socket that your GUI-less process
is listening on and from there intiate the shutdown process. In other
words, in your GUI-less program:

* fire up a thread that listens on a specific port for signal
and performs all the necessary shutdown stuff you want.

* add code that sends the signal to the port when the parameter
'stop' is passed.
 
P

Paul Lutus

Martin said:
I would kill it with kill -9 as I don't know any other way. The shutdown
hook may be an idea, but it only seems to work with normal
shutdown-procedures.

How does programs normally run idly in the background without GUI's in
Java.

They do it the same way that Linux daemons do it -- by surrendering their
time slice while idle, but not their thread.
I want to be able to have it running, and write

myApp stop

to have it gracefully shut down.

You need to write your application so that it tests some kind of external
flag and shuts itself down on detection of the flag (and this is
OS-specific, not strictly speaking a Java issue). This is because killing
threads externally is dangerous. It is much better to send a signal that
causes the thread to shut itself down. This also allows a cleanup if
needed, which an external kill command doesn't do unless the applicaiton
traps and acts on the kill command (also not strictly about java).

Example (not tested):

Runnable update = new Runnable() {
public void run() {
File f = new File("path to flag file");
while (f.exists()) {
// do something
}
cleanup();
}
};
update.run();
 
P

Paul Lutus

GaryM said:
Agreed, but kill -SIGTERM does work and this is a nice way to shut the
process down, which is what OP wanted I thought.

But that is OS-specific, and the topic is a Java application that won't be
handling the signal directly, therefore it cannot clean up before exiting.
 
G

GaryM

But that is OS-specific, and the topic is a Java application that
won't be handling the signal directly, therefore it cannot clean
up before exiting.

The OP asked, "How can I send a signal to the program? Can a seperate
program do this?". From the source he posted it looks like the app
has a method to handle the the shutdown. Therefore the topic as I
interpreted it was, how do I send signal to the app to get it to
shutdown cleanly. He mentioned he used kill -9 so I just give him a
*nix alternative that would execute the shut down hook. The are
similar ways for Windows, OS/400 etc that execute the shut down
hook.

For the OP, here is a good article I found that might help you:

http://www.smotricz.com/kabutz/Issue043.html
 
Z

zoopy

I would kill it with kill -9 as I don't know any other way. The shutdown
hook may be an idea, but it only seems to work with normal
shutdown-procedures.

IIRC then is kill -9 == kill -SIGKILL, so it's likely that the shutdown hook will not be executed.
How does programs normally run idly in the background without GUI's in
Java. I want to be able to have it running, and write

myApp stop

to have it gracefully shut down.

Probably there are many ways, but one way is to have two threads, one thread does the actual work
until it's signalled to stop. The second thread is listening to the outside world (you) for some
sign to stop. When it receives this sign it signals the first thread, which then can cleanup
gracefully.

Below I've included a very rough framework of how that might look like.

The sign from the outside world can be realized in various ways.

One way would be to test for the existance of a file in a directory (while that file doesnt exist,
continue working).

A second, an probable more reliable way is to listen on a socket for a certain message. IIC this is
how Tomcat has realized its shutdown mechanism.
Then I don't think the shutdownhook will help. I agree.


Martin Jensen

public class App
{
public static void main(args)
{
Worker worker = new Worker()
ShutdownListener shutdownListener = new ShutdownListener(worker);

Thread shutDownThread = new Thread(shutdownListener);
Thread workerThread = new Thread(worker);

shutDownThread.start();
workerThread.start();
}
}

public class Worker
implements Runnable
{
private boolen working = true;
public void run()
{
while (isWorking())
{
WTD someWork = getSomeWorkToDo();
someWork.doIt();
}
cleanUp();
}
public synchronized boolean isWorking()
{
return working;
}
public synchronized void setWorking(boolean working)
{
this.working = working;
}
private void cleanUp()
{
// ...
}
private WTD getSomeWorkToDo()
{
// ...
}
}



public class ShutdownListener
implements Runnable
{
private Worker worker;
public ShutdownListener(Worker worker)
{
this.worker = worker;
}

public void run()
{
while( ! workerShouldStop() )
{
Thread.sleep(500);
}
worker.setWorking(false);
}

private boolean workerShouldStop()
{
//--------
// get sign from the outside world
//--------

// various implementation possible
}
}
 
M

Martin Jensen

GaryM said:
The OP asked, "How can I send a signal to the program? Can a seperate
program do this?". From the source he posted it looks like the app
has a method to handle the the shutdown. Therefore the topic as I
interpreted it was, how do I send signal to the app to get it to
shutdown cleanly. He mentioned he used kill -9 so I just give him a
*nix alternative that would execute the shut down hook. The are
similar ways for Windows, OS/400 etc that execute the shut down
hook.

For the OP, here is a good article I found that might help you:

http://www.smotricz.com/kabutz/Issue043.html

Thanks, I think the shutDownHook is the way to go as many other posters have
mentioned.


Martin Jensen
 
A

Andrew Thompson

...
I know, sorry about that.

I'm over it. Especially since *.
..It's just that I got a tip to post here after I
originally posted on another group

If you should make such a mistake in future, it
might pay to mention 'previously posted in ....
under subject ...., reposting here because ...'
..since It would be more likely that I got
more responses here. Which was true.

I'm glad you got your answer Martin! **
..I'll just post here in the future...

* This is the important part, one group only.

My Thanks.

** I also noticed you displayed the common sense
to report the solution on the original thread.
Very thoughtful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top