java在centos中,以daemon方式启动

以下内容基于Centos 6.3版本。

java开发的时候,需要注意以下几点:

  1. 在main中,建立额外线程以保证一直运行;
  2. 注册runtime的shutdownhook,以执行服务线程的退出前清理工作;

其他内容,都用shell脚本来控制,主要有:

$APP_HOME/bin/daemon.sh,该shell,支持link到服务目录,负责协调服务。

 1 #!/bin/sh
 2 #
 3 # chkconfig:   - 20 80
 4 # description: Starts and stops the redis daemon.
 5 #
 6 # Source function library.
 7 ### BEGIN INIT INFO
 8 # Required-Start:    $local_fs $remote_fs $network $time $named
 9 # Required-Stop:     $local_fs $remote_fs $network $time $named
10 # Default-Start:     3 5
11 # Default-Stop:      0 1 2 6
12 ### END INIT INFO
13 
14 NAME="sangame-daemon"
15 APP_USER="root"
16 
17 . /etc/rc.d/init.d/functions
18 ulimit -HSn 65534
19 
20 PRG="$0"
21 
22 while [ -h "$PRG" ]; do
23   ls=`ls -ld "$PRG"`
24   link=`expr "$ls" : '.*-> (.*)$'`
25   if expr "$link" : '/.*' > /dev/null; then
26     PRG="$link"
27   else
28     PRG=`dirname "$PRG"`/"$link"
29   fi
30 done
31 
32 # Get standard environment variables
33 PRGDIR=`dirname "$PRG"`
34 
35 APP_HOME=`cd "$PRGDIR/.." >/dev/null; pwd`
36 
37 APP_EXEC="$APP_HOME/bin/startup.sh"
38 #NAME="sangame-daemon-1"
39 #$APP_HOME="/data/home/sangame/sangame-server/daemon-1"
40 #APP_USER="sangame"
41 #shfile="$apphome/bin/startup.sh"
42 #exec="sh $shfile"
43 APP_PIDFILE="$APP_HOME/bin/app.pid"
44 
45 #[ -e /etc/sysconfig/redis ] && . /etc/sysconfig/redis
46 
47 start() {
48     echo "Starting $NAME: $APP_HOME"
49     daemon --user $APP_USER --pidfile $APP_PIDFILE $APP_EXEC
50     retval=$?
51     [ $retval -eq 0 ]
52     return $retval
53 }
54 
55 stop() {
56     echo $"Stopping $NAME: $APP_HOME"
57     killproc -p $APP_PIDFILE $NAME
58     retval=$?
59     echo
60     [ $retval -eq 0 ]
61     return $retval
62 }
63 
64 restart() {
65     stop
66     start
67 }
68 
69 rh_status() {
70     echo $"Status $NAME: $APP_HOME"
71     status -p $APP_PIDFILE $NAME
72 }
73 
74 rh_status_q() {
75     rh_status >/dev/null 2>&1
76 }
77 
78 case "$1" in
79     start)
80         rh_status_q && exit 0
81         $1
82         sleep 5s
83         rh_status
84         ;;
85     stop)
86         rh_status_q || exit 0
87         $1
88         ;;
89     restart)
90         $1
91         ;;
92     status)
93         rh_status
94         ;;
95     *)
96         echo $"Usage: $0 {start|stop|status|restart}"
97         exit 2
98 esac
99 exit $?
View Code

$APP_HOME/bin/startup.java,该shell,主要负责启动java主程序,同时设置pid文件。

 1 #!/bin/sh
 2 #
 3 ulimit -HSn 65534
 4 
 5 PRG="$0"
 6 
 7 while [ -h "$PRG" ]; do
 8   ls=`ls -ld "$PRG"`
 9   link=`expr "$ls" : '.*-> (.*)$'`
10   if expr "$link" : '/.*' > /dev/null; then
11     PRG="$link"
12   else
13     PRG=`dirname "$PRG"`/"$link"
14   fi
15 done
16 
17 # Get standard environment variables
18 PRGDIR=`dirname "$PRG"`
19 
20 APP_HOME=`cd "$PRGDIR/.." >/dev/null; pwd`
21 
22 cd $APP_HOME
23 
24 JVM_START="-server -Xms256m -Xmx512m -Xss256k -XX:MaxPermSize=256m -XX:+UseParallelGC -XX:ParallelGCThreads=2 -XX:+UseParallelOldGC  -XX:MaxGCPauseMillis=100 -XX:+UseAdaptiveSizePolicy -verbose:gc -Xloggc:log/gc.log -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+PrintTenuringDistribution"
25 
26 CLASSPATH="`for i in $APP_HOME/lib/*.jar ; do echo -n $i: ; done`."
27 
28 LOGBACK_CONFIG_FILE="config/logback.xml"
29 
30 echo "Using APP_HOME:                  $APP_HOME"               > ${APP_HOME}/log/app.out
31 echo "Using CLASSPATH:                 $CLASSPATH"              >> ${APP_HOME}/log/app.out
32 echo "Using JVM_START:                 $JVM_START"              >> ${APP_HOME}/log/app.out
33 echo "Using LOGBACK_CONFIG_FILE:       $LOGBACK_CONFIG_FILE"    >> ${APP_HOME}/log/app.out
34 
35 java -classpath $CLASSPATH $JVM_START com.baolemon.sangame.server.rts.AppMain >> ${APP_HOME}/log/app.out 2>&1 &
36 
37 echo -n $! > ${APP_HOME}/bin/app.pid
View Code

脚本注册为服务的代码:

 1 cd /etc/init.d/
 2 ln -s $APP_HOME/bin/daemon.sh xxx-daemon-n  #连接shell脚本
 3 chkconfig --add xxx-daemon-n
 4 chkconfig xxx-daemon-n on   #设置为自动启动
 5 
 6 #测试效果
 7 service xxx-daemon-n start
 8 service xxx-daemon-n status
 9 service xxx-daemon-n stop
10 service xxx-daemon-n restart
原文地址:https://www.cnblogs.com/jdragonhu/p/3315046.html