以下内容基于Centos 6.3版本。
java开发的时候,需要注意以下几点:
- 在main中,建立额外线程以保证一直运行;
- 注册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 $named10 # Default-Start: 3 511 # Default-Stop: 0 1 2 612 ### END INIT INFO13 14 NAME="sangame-daemon"15 APP_USER="root"16 17 . /etc/rc.d/init.d/functions18 ulimit -HSn 6553419 20 PRG="$0"21 22 while [ -h "$PRG" ]; do23 ls=`ls -ld "$PRG"`24 link=`expr "$ls" : '.*-> \(.*\)$'`25 if expr "$link" : '/.*' > /dev/null; then26 PRG="$link"27 else28 PRG=`dirname "$PRG"`/"$link"29 fi30 done31 32 # Get standard environment variables33 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/redis46 47 start() {48 echo "Starting $NAME: $APP_HOME"49 daemon --user $APP_USER --pidfile $APP_PIDFILE $APP_EXEC50 retval=$?51 [ $retval -eq 0 ]52 return $retval53 }54 55 stop() {56 echo $"Stopping $NAME: $APP_HOME"57 killproc -p $APP_PIDFILE $NAME58 retval=$?59 echo60 [ $retval -eq 0 ]61 return $retval62 }63 64 restart() {65 stop66 start67 }68 69 rh_status() {70 echo $"Status $NAME: $APP_HOME"71 status -p $APP_PIDFILE $NAME72 }73 74 rh_status_q() {75 rh_status >/dev/null 2>&176 }77 78 case "$1" in79 start)80 rh_status_q && exit 081 $182 sleep 5s83 rh_status84 ;;85 stop)86 rh_status_q || exit 087 $188 ;;89 restart)90 $191 ;;92 status)93 rh_status94 ;;95 *)96 echo $"Usage: $0 {start|stop|status|restart}"97 exit 298 esac99 exit $?
$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; then11 PRG="$link"12 else13 PRG=`dirname "$PRG"`/"$link"14 fi15 done16 17 # Get standard environment variables18 PRGDIR=`dirname "$PRG"`19 20 APP_HOME=`cd "$PRGDIR/.." >/dev/null; pwd`21 22 cd $APP_HOME23 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.out31 echo "Using CLASSPATH: $CLASSPATH" >> ${APP_HOME}/log/app.out32 echo "Using JVM_START: $JVM_START" >> ${APP_HOME}/log/app.out33 echo "Using LOGBACK_CONFIG_FILE: $LOGBACK_CONFIG_FILE" >> ${APP_HOME}/log/app.out34 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
脚本注册为服务的代码:
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 stop10 service xxx-daemon-n restart