以下内容基于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

相关文章:

  • 2021-12-14
  • 2021-11-16
  • 2022-12-23
  • 2020-11-02
  • 2022-12-23
  • 2022-12-23
  • 2021-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-11
  • 2021-05-08
  • 2022-02-22
  • 2021-06-14
  • 2021-09-12
  • 2021-10-14
  • 2021-11-09
相关资源
相似解决方案