How to convert an existing Java application to a SYS V service (daemon) [duplicate]

风流意气都作罢 提交于 2019-11-29 04:17:10

Take a look at Apache Commons Daemon.

It has 'jsvc' launcher which suports starting and stopping java-based daemons (services).

Firstly, Saving the PID on *nix:

$ ./yourprogram &
$ echo $! > /var/run/yourpid

yourpid will now contain yourpgram's PID, and /var/run is the proper place to put it.

The above can be put in your "start" script. The "stop" script can look at yourpid to know what to kill.

If you want to be more elegant and stop your app properly, you can look at the source code for Tomcat's org.apache.catalina.startup.Catalina.java on how to implement proper shutdown hooks.

Secondly, above "stop" and "start" scripts can then be put in /etc/init.d/mystopstartscript:

#!/bin/bash
# processname: yourprogram
# pidfile: /var/run/yourpid

case $1 in
start)
        sh /some/where/start.sh
        ;;
stop)  
        sh /some/where/stop.sh
        ;;
restart)
        sh /some/where/stop.sh
        sh /some/where/start.sh
        ;;
esac   
exit 0

This is a fairly home-grown solution, with ideas mostly taken from good 'ol Tomcat, but I hope it helps :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!