How to stop java service with command line parameter '-stop'?

W

Will

I've got a java service under solaris which runs in the background
(command line based). When I start the application it runs and
periodically executes some stuff.

Questions:
==========
1. Is there an easy and quick way to stop the application/service
instead of killing the process from within the shell (which is not
really nice).

2. How can I stop it using java code?
Is there any other way than listening to a socket or monitoring a file
for some kind of shutdown command? (interprocess communication)

2. How can I avoid that more than one instance of the application run
at the same time?


Here's the code I use so far:
=============================

public final class NuevMailTimer extends TimerTask{

public static void main(String[] args) {
if (args.length!=1) {
System.out.println("ERROR: Missing argument");
System.out.println("Usage: XXXX -start|stop");
System.exit(1);
}

if (args[0].trim().toLowerCase().equals("-stop")) {
System.out.println("stopped!");
// WHAT TO PUT IN HERE??
}

if (!args[0].trim().toLowerCase().equals("-start")) {
System.out.println("ERROR: Unknown argument");
System.out.println("Usage: XXXX -start|stop");
System.exit(1);
}


...

Long pollInterval = new Long(poll_interval);
final long TIMER_INTERVAL = pollInterval.longValue();

TimerTask tTask = new NuevMailTimer();
Timer myTimer = new Timer();
myTimer.scheduleAtFixedRate(tTask, 0, TIMER_INTERVAL);
}


public void run() {
...
}
}


Will
 
T

Thomas Weidenfeller

Will said:
I've got a java service under solaris which runs in the background
(command line based). When I start the application it runs and
periodically executes some stuff.

You could use cron instead to periodically execute some program.
1. Is there an easy and quick way to stop the application/service
instead of killing the process from within the shell (which is not
really nice).

kill or better pkill (with SIGTERM, not SIGKILL) is the easy, quick and
clean way. If you need to do cleanup work in the Java program, add a
shutdown hook.

If you don't like to type the [p]kill command line, wrap it in a small
shell script.
2. How can I stop it using java code?
Is there any other way than listening to a socket or monitoring a file
for some kind of shutdown command? (interprocess communication)

I am not aware of anything simple. If you want to have something
complex, you could tap into the VM via the debugging API (has been
changed in 1.5 to the tool API) and terminate the VM. But that would be
more brutal than kill.
2. How can I avoid that more than one instance of the application run
at the same time?

(a) Try opening some application-specific port. If the port is in use,
your application is already running.

(b) Use a lock file. When the file is already locked, assume your
application is already running. This can only be done reliable with
recent java versions, where you have some atomic operation for
checking/locking (FileChannel.tryLock()).

/Thomas
 

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,020
Latest member
GenesisGai

Latest Threads

Top