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

Discussion in 'Java' started by Will, Nov 2, 2004.

  1. Will

    Will Guest

    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
    Will, Nov 2, 2004
    #1
    1. Advertising

  2. Will wrote:
    > 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
    Thomas Weidenfeller, Nov 2, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Karsten W.

    py2exe, command line parameter problem

    Karsten W., Aug 9, 2007, in forum: Python
    Replies:
    0
    Views:
    388
    Karsten W.
    Aug 9, 2007
  2. asit
    Replies:
    1
    Views:
    272
    Thorsten Kampe
    Jan 18, 2009
  3. tarekeldeeb
    Replies:
    4
    Views:
    10,776
    tarekeldeeb
    Mar 10, 2009
  4. curious
    Replies:
    1
    Views:
    249
  5. soren625
    Replies:
    10
    Views:
    219
    Kevin Collins
    Dec 28, 2005
Loading...

Share This Page