1

I have a simple Java program running in a Linux box, and the program basically schedules a recurring task that runs for example every hour, so it is kind of like a Daemon thread sitting there but not quite. I am wondering how do know if the program is still running in Linux as well as how to terminate the program running ? Do I need to find a way to trigger the cancel method ? or I can just kill it by using Linux command ? or I need to write a stop and run script ? I am not sure if this could be an open-ended question, but what's the best approach in practice.

Here's my java code

        public static void main(String [] args){
               ScheduledExecutorService task = 
                                 Executors.newSingleThreadScheduledExecutor();

               task.scheduleAtFixedRate(new Task(),0,1,TimeUnit.HOURS);
        }

        static class Task{
             @Override
             public void run(){
                    //do something
             }
        }
peter
  • 8,333
  • 17
  • 71
  • 94
  • You could just stop it from the command line, CTRL+C if I'm right (not a Linux user - Mac) – 11684 Nov 13 '12 at 16:41
  • I am wondering will it be bad if we use CTRL+C because that will kill it right away, and what happen if we have some tasks that is processing in the middle ? do you lost that information ? – peter Nov 13 '12 at 16:55
  • No, except for when you are doing IO/network communication etc. I believe you can even listen to that event so you can do some clean-up before you get killed. – 11684 Nov 13 '12 at 16:58
  • How do you add listener to that. Can you give some details – peter Nov 13 '12 at 17:01
  • 1
    I think this should work: http://stackoverflow.com/questions/914666/how-to-capture-system-exit-event – 11684 Nov 13 '12 at 17:39
  • Ctrl-C can be inhibited (it is SIGINT) see on my answer. – Daniel Voina Nov 14 '12 at 08:43
  • @DanielVoina What do you mean 'inhibited' – peter Nov 14 '12 at 15:09
  • Any signal can be ignored by the receiving application by setting its handler to to SIGIGN, hence it will not treat it. It is not possible though to ignore signals as SIGKILL, SIGHUP. Please have a look on http://web.eecs.utk.edu/~huangj/cs360/360/notes/Signals/lecture.html if you need more details. It is mostly out of the scope of this question. – Daniel Voina Nov 14 '12 at 16:20

3 Answers3

1

Use a signal handler via (sun.misc.Signal). In case you get a signal handle it by calling shutdown() on the executor.

EG:

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import sun.misc.Signal;
import sun.misc.SignalHandler;

public class Main {
    public static void main(String[] args) {
        final ScheduledExecutorService task = Executors
                .newSingleThreadScheduledExecutor();

        task.scheduleAtFixedRate(new Task(), 0, 1, TimeUnit.HOURS);
        SignalHandler sh = new SignalHandler() {

            @Override
            public void handle(Signal arg0) {
                task.shutdown();
            }
        };

        Signal.handle(new Signal("USR1"), sh);
    }

    static class Task implements Runnable {
        @Override
        public void run() {
            // do something
        }
    }
}
Daniel Voina
  • 3,185
  • 1
  • 27
  • 32
  • How do you do that ? Will that be a separate thread running in a separate application ? – peter Nov 13 '12 at 16:48
  • Handle USR1 and externally run "kill -USR1 " – Daniel Voina Nov 13 '12 at 17:59
  • so USR1 could be any strings you want correct ? and once we have Signal.handle(new Signal("USR1"),sh); we can simply run that Lunix command to kill it, correct ? – peter Nov 14 '12 at 02:37
  • No, there is a set of available signals in Unix-like systems (eg. USR1, USR2, PIPE, CHLD, ...). Have a look on http://www.ibm.com/developerworks/java/library/i-signalhandling/ for a better explanation – Daniel Voina Nov 14 '12 at 08:41
1

There are many ways.

Google for ipc (inter process communications).

... and/or program the cron to stop your process from time to time...

alinsoar
  • 15,386
  • 4
  • 57
  • 74
1

how do know if the program is still running in Linux as well as how to terminate the program running?

If you are talking about existing code then you are SOL unless there already are hooks built into the program.

what's the best approach in practice.

There are many ways to accomplish this. I'd use JMX. Just expose a JMX operation which calls the cancel() method to stop the timer task thread. There are a number of different JMX command-line clients and you can always use jconsole.

How to expose the JMX method depends a lot on what framework you are using. You could use my SimpleJmx library to accomplish this fairly easily.

Gray
  • 115,027
  • 24
  • 293
  • 354