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.

  • 相关阅读:
    一个貌似比较吊的递归转换为loop--总算成功了.
    为何反转迭代顺序就不会栈溢出了?
    将树形递归转换为loop
    递归和迭代之间的转换简单例子
    非线性递归函数转化为迭代函数举例
    将尾递归函数转换为迭代函数的利器
    转:LINUX/UNIX下的回车换行与WINDOWS下的区别
    property干嘛的
    eval和列表解析的一处陷阱
    剑指offer——16二进制中1的个数
  • 原文地址:https://www.cnblogs.com/lexus/p/2363987.html
Copyright © 2011-2022 走看看