Question:
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:
1 2 |
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?
Answer:
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:
1 2 3 4 5 6 |
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.