简体   繁体   中英

Run Java ScheduledExecutorService in the background

I have jar file that runs a process every two minutes. It works perfectly but if someone closes the terminal the process is terminated. I am looking at a way of executing it as a background process. Here is my main:

public class SMSAlert {

    private static ScheduledExecutorService scheduler;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        scheduler = Executors.newSingleThreadScheduledExecutor();
        Alert alert = new Alert();
        scheduler.scheduleAtFixedRate(alert, 0, 2, TimeUnit.MINUTES);
    }

}

On linux, you can avoid the 'closing terminal kills process' problem by using the screen command. This detaches the process from being affected by what happens to the terminal.

Another solution to your problem would be to run your application through Cron . Once again, this makes its execution independent of any particular terminal session.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM