Wednesday, February 23, 2022

[SOLVED] How to run .jar continually and automatically on Debian server?

Issue

I have a .jar file I want to run all the time on a Debian server.

Currently I have figured out how to access the server via ssh on Cygwin and start the .jar. But when I close the Cygwin window on my development machine, it kills the process on the server (I think, as it's not responding any longer).

Currently I start it like this:

java -jar myjar.jar packageName.fileNameOfFileWithMainMethod

I need to make this file run automatically and continually on the server (it is an integral part of the system I am developing).

Unfortunately I know next to nothing about server management, or non-windows operating systems in general (wasn't me who chose or made the server), so I really don't know what to do, nor what to search for (apparently, since my searching gave no usable results).

I have read (and edited because the text was a mess) this question, but although I feel it might be hinting in the right direction, I didn't get much help from it. I also tried my best googlefu, but it got me a lot of only tangentially related results.

I guess I'll have to make some kind of script (possibly containing the code line above), do some stuff to it and put it somewhere specific on the server to accomplish what I want to do.

Would someone be so kind as to explain how this is done?


Solution

One simple suggestion would be to run the jar file using Linux's CRON.

This article from unix stack exchange should get you going the correct direction for running a jar file using cron.

Alternatively, this article from mkyong.com is also clear and concise.

For example:

  1. Connect using Cygwin
  2. Run crontab -e
  3. Enter 0 * * * * java -jar myjar.jar packageName.fileNameOfFileWithMainMethod to run the jar file every hour at the top of the hour. Or, to start it once on server startup enter @reboot java -jar myjar.jar packageName.fileNameOfFileWithMainMethod


Answered By - Tom Mac
Answer Checked By - Willingham (WPSolving Volunteer)