peter
peter

Reputation: 8473

How to kill and stop the TimerTask running outside the application in Linux

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
             }
        }

Upvotes: 1

Views: 1572

Answers (3)

Gray
Gray

Reputation: 116888

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.

Upvotes: 1

Daniel Voina
Daniel Voina

Reputation: 3215

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
        }
    }
}

Upvotes: 1

alinsoar
alinsoar

Reputation: 15793

There are many ways.

Google for ipc (inter process communications).

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

Upvotes: 1

Related Questions