1

Is there any way to schedule to stop or terminate a java program on a specific time. I am using java.util.timer. For example at 10pm. I know how to schedule start a java program on a specific time. Problem is I cannot figure out a way to terminate a program.

D.Madu
  • 507
  • 2
  • 8
  • 28
  • System.exit(1) stops a java program. But i don't know if it is the best way... – Benjamin Schüller Apr 28 '17 at 13:30
  • 1
    "I know how to schedule start a java program on a specific time" How about using the same method to schedule a `kill -SIGTERM` at the desired time? – Andy Turner Apr 28 '17 at 13:30
  • I want to know how to schedule that..for ex stop at 10pm.. my bad i didn't mention it in the question. I updated my question.sorry – D.Madu Apr 28 '17 at 13:37
  • You want to stop a "remote" Java application, and not your application? – Benjamin Schüller Apr 28 '17 at 13:41
  • Calendar today = Calendar.getInstance(); today.set(Calendar.HOUR_OF_DAY, 18);//set to 18 which is 6:00PM in 24 HRs today.set(Calendar.MINUTE, 00); today.set(Calendar.SECOND, 0); // every night at 2am you run your task Timer timer = new Timer(); timer.schedule( , today.getTime()); class CloseApp implements Runnable{ public void run(){ System.exit(0); }}} – Mwesigye John Bosco Apr 28 '17 at 13:41
  • or use cron job in linux 00 18 * * * /etc/init.d/application stop – Mwesigye John Bosco Apr 28 '17 at 13:43
  • @benjamin ... stop my application – D.Madu Apr 28 '17 at 13:49

5 Answers5

2

You can create a background thread (in order to not block UI) and inside you can check current hour/minutes/day

GregorianCalendar calendar = new GregorianCalendar (); // creates a new calendar instance 
calendar.get(Calendar.HOUR_OF_DAY); // gets hour in 24h format
calendar.get(Calendar.HOUR);        // gets hour in 12h format
calendar.get(Calendar.MONTH);       // gets month number, NOTE this is zero based!
Thread.sleep(60000)                 //sleep 60seconds

if(<rightTime>)
    System.exit(0);

See also https://stackoverflow.com/a/907207/6726261

Community
  • 1
  • 1
Maurizio Ricci
  • 462
  • 1
  • 4
  • 11
1

You can use TimerTask to do that, here is an example:

  import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

public class StopApp {
Timer timer = new Timer();
TimerTask StopApp = new TimerTask() {
    @Override
    public void run() {
        System.exit(0);
    }
};
public StopApp() {

//timer.schedule(exitApp, getDateDiff(new Date("get the actual time"), new Date("get the time you want to stop your app"), TimeUnit.SECONDS));
    //Example
    timer.schedule(StopApp, new Date(System.currentTimeMillis()+2*1000));//Exits after 2 sec of starting the app

while(true)

    System.out.println("The App still turn");
}

public static Date getDateDiff(Date date1, Date date2, TimeUnit timeUnit) {
    long diffInMillies = date2.getTime() - date1.getTime();
    Date date=new Date(diffInMillies);

    return date;//timeUnit.convert(diffInMillies,TimeUnit.MILLISECONDS);
}

public static void main(String[] args) {

    new StopApp();

}
}
Yacino
  • 645
  • 2
  • 10
  • 32
0

Check this doc: https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#exit(int). System.exit(0); can do what you want.

rjsperes
  • 65
  • 1
  • 8
  • I want to know how to schedule that..for ex stop at 10pm.. my bad i didn't mention it in the question. I updated my question.sorry – D.Madu Apr 28 '17 at 13:37
  • D.Madu said that he was using java.util.timer so i think that he already know how to schedule a task (Sytem.exit(0) in this case). – rjsperes Apr 28 '17 at 13:39
  • D.Madu check the answer of nate_weldon to this question: http://stackoverflow.com/questions/15747277/how-to-make-java-program-exit-after-a-couple-of-seconds. It may help you. – rjsperes Apr 28 '17 at 13:40
0

To stop your program use: System.exit(0); More info here

kutsyk
  • 185
  • 1
  • 13
  • You are missing the _on a specific time_ part of the question – AxelH Apr 28 '17 at 13:34
  • @AxelH `I know how to schedule start a java program on a specific time. Problem is I cannot figure out a way to terminate a program.` – kutsyk Apr 28 '17 at 13:35
  • I want to know how to schedule that..for ex stop at 10pm.. my bad i didn't mention it in the question. I updated my question.sorry – D.Madu Apr 28 '17 at 13:37
0

You could use procrun, which will create a service which will run your java application. Then you can schedule start and stop of your service.

You should read the docs of procrun which explain how to configure your service.

The easier way to do this is just to run prunsrv //IS//my_service_name which creates the service, then you execute prunmngr //ES//my_service_name which will open a service configuration dialog where you can set up all service parameters.

Your application will need a public static method to call which will be used by procrun to stop your application.

Another option is to read from a stream and wait for some input, when input is read you stop the application.

This stream could be a socket or a file, then when you want to stop your app, you just schedule another application to write on that stream.

minus
  • 2,646
  • 15
  • 18