zoukankan      html  css  js  c++  java
  • Running Java applications as daemon on Debian Linux

    Running Java applications as daemon on Debian Linux

    Running Java applications as daemon on Debian Linux

    Debian has start-/stop scripts in /etc/init.d/ which are documented in the Debian Policy Manual. An example is available in /etc/init.d/skeleton. These scripts use start−stop−daemon to start and stop a background process. So to configure a start/stop script for a Java application, copy the skeleton f.e. to /etc/init.d/myserver and customize the settings:

    ...
    
    # Required-Start:    $local_fs $remote_fs $network
    # Required-Stop:     $local_fs $remote_fs $network
    
    ...
    
    DESC="My Server"
    NAME=myserver
    DAEMON=/opt/myserver/bin/$NAME
    ...

    Then update-rc.d is executed to update the scripts for the run levels:

    update-rc.d myserver defaults

    If the application needs additional arguments (like log4j.configuration) I recommend to created a little wrapper script like bin/myserver so you don’t need to keep this configuration in the init.d script:

    #!/bin/sh
    
    cd $(dirname $0)/../
    exec java -Dlog4j.configuration=file:./conf/log4j.properties -jar bin/felix.jar

    The exec here is important, it makes the started java process replace the script process, so that a) the process id doesn’t change and b) we don’t get an additional process because of the script.

    The default skeleton start/stop script expects the Java application to behave like a proper daemon: it should detach itself from the parent process. There is a way to make a Java application do this: jsvc. For this to work, the software needs to implement a Daemon interface from Commons Daemon. The big advantage of this approach is that the Java process can perform actions as root (for example open privileged ports) before switching to a non-root-user.

    If you don’t want to go that way, there is a “last resort way” to work around this issue using options for start-stop-daemon:

    • Configure start-stop-daemon to run the process as the given user, detach the process and automatically create a pid-file (I added EXTRA_ARGS to all calls to start-stop-daemon):
    EXTRA_ARGS="-c <myuser> --background --make-pidfile"
    • The skeleton assumes that the process will be an instance of the command executed (not true because of the wrapper script) and not stop the process (because it thinks none is running). This can be fixed by replacing --exec with --startas in the script.

    • The skeleton also assumes the process will be named $NAME, which is not true, this can be fixed by removing --name $NAME.

  • 相关阅读:
    hdu6162 Ch’s gift
    hdu 6166 Senior Pan
    bzoj2818 Gcd
    hdu6127 Hard challenge
    splay伸展树模板
    2016-2017 National Taiwan University World Final Team Selection Contest J
    2016-2017 National Taiwan University World Final Team Selection Contest C
    使用powershell管理域用户
    EGL Driver message (Critical) eglInitialize: No available renderers.
    Python实现telnet命令测试防火墙
  • 原文地址:https://www.cnblogs.com/lexus/p/2363987.html
Copyright © 2011-2022 走看看