Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep a java program running 24/7?

I made a Java program and made it a jar file. I want to make it running 24/7 on my AWS. I use an application called screen to keep it running after I log out. I use this command:

screen java -jar my_app.jar

But it cannot keep running all the time. It happens to stop after running for one or two days. I made keep a log, and I didn't find any exception to terminate it. I wonder if the JVM will kill my application for any reason, such as high memory usage? Does the JVM keep a log? Where can I find the log of JVM? Do I have to make my app a deamon ? If I have to, how?

like image 865
user2452323 Avatar asked Oct 14 '13 02:10

user2452323


2 Answers

No, there's nothing special to keep a Java app running. Just have at least one non-daemon thread still active. For instance, this is a complete program that would run forever:

public class Forever {
    public static void main(String[] args) throws InterruptedException {
        while (true) { Thread.sleep(1000); }
    }
}

If your app is dying, it's because of some error somewhere. Make sure to inspect whatever logs are being written and the stdout and stderr of the process to find the cause.

like image 119
Ryan Stewart Avatar answered Sep 28 '22 00:09

Ryan Stewart


maybe you found a solution, but on windows, you can use a batch "watchdog" to re-launch your program when it closes (and also log data).

:launch
java -jar my_app.jar 1>>log.txt 2>>err.txt
goto launch

on linux/unix you can make a bash along those lines (I'm too lazy to do a 2 min search) also, a good thing would be to monitor your app's memory usage, and close it if it is over a threshold (like 70%), it will be re-launched by the ba*h script

like image 23
Mana Quri Avatar answered Sep 27 '22 23:09

Mana Quri