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.

  • 相关阅读:
    升级安装 Ubuntu 后该做的20项优化工作
    Delphi中点击DBGrid某一行获得其详细数据方法
    android eclipse xml不自动代码提示
    区别不同浏览器,CSS hack写法
    自我介绍
    秋季学期学习总结
    人生路上影响最大的三位老师
    SQL优化34条
    Visual Studio 2010将支持多种架构设计图
    Visual Studio 2010的SharePoint工作流功能
  • 原文地址:https://www.cnblogs.com/lexus/p/2363987.html
Copyright © 2011-2022 走看看