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

后端 未结 3 957
谎友^
谎友^ 2021-01-25 22:48

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

相关标签:
3条回答
  • 2021-01-25 23:18

    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).

    0 讨论(0)
  • 2021-01-25 23:27

    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.

    0 讨论(0)
  • 2021-01-25 23:36

    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
    0 讨论(0)
提交回复
热议问题