How to run .jar continually and automatically on D

2020-05-06 16:26发布

问题:

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?

回答1:

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


回答2:

Shell hooks are good for configuring user environment variables.

Cron is for scheduled jobs, mostly related maintenance, such as creating backups, managing log-files etc ...

Background processes with nohup as advised by Николай Митропольский, or ssh with "screen" application (which let you detach/reattach to a "session"), will be useful in development time.
But can not handle server shutdown cleanups, or respond to restarts.

Init scripts mentioned above is the standard way to start/stop services.

There is an application named init, which is the first application started when a Unix-like system boots.
Init, according to runlevel, starts some scripts, and those scripts manages daemons (services in Windows).
So for services, you write "hooks" for runlevels,

In Debian, /etc/init.d/ where you put your init scripts,
you can read the scripts inside this folder to get the idea,
they are text files (bash scripts).

Those scripts are called with an argument
(a standard keywords, such as "start", "stop" etc..).

/etc/rc?.d/ (where ? is one of runlevels), where the init finds the scripts to run.
But those scripts are just "automatically created" symbolic links to the scripts in /etc/init.d/.
You do not need to touch anything inside /etc/rc?.d/ folder.
*After putting your script into /etc/init.d/,
you only need to call to create symbolic links *:

sudo update-rc.d "your-scripts-name" defaults

As you see there are some prefixes attached to names of scripts; for example /etc/rc1.d/K10apache2 which is a symbolic link to /etc/init.d/apache2.
So a simple "ordered by name execution" is possible here.

Automatically creating those prefixes (so the execution order), dependency information is required.
So init scripts includes this information. Also information required when (at which runlevel) those scripts should be called (with "start" or "stop").

This dependency information is placed as comments in those scripts. Forexample apache server init script (/etc/init.d/apache2) includes those lines;

# Provides:          apache2
# Required-Start:    $local_fs $remote_fs $network $syslog $named
# Required-Stop:     $local_fs $remote_fs $network $syslog $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6

Detailed information exists in Debian policy;
https://www.debian.org/doc/debian-policy/ch-opersys.html#s-sysvinit also this will be usefull; https://refspecs.linuxbase.org/LSB_2.1.0/LSB-generic/LSB-generic/iniscrptfunc.html

NOTE: There is a huge transition and debates/fragmentations in Unix world.
Init and init scripts, tradationally used in Unix/Unix-like systems, nowadays becoming obsolete on many systems.
https://en.wikipedia.org/wiki/Init#Replacements_for_init
Debian currently uses systemd, but init scripts still works with systemd (systemd provides the compatibility).



回答3:

Simplest solution is to detach process by using nohup with &:

nohup java -jar myjar.jar packageName.fileNameOfFileWithMainMethod &

to stop process will be possible with kill <process-id> command

process id could be found by ps -ef | grep packageName.fileNameOfFileWithMainMethod

But if you are developing serious application that is long-running on server you have to deal with initialization system like systemd, upstart or something like that.