简单介绍:

说明: Jsvc主要用于非Windows操作系统上以非ROOT用户运行绑定到特权端口的Java服务器,专门由C编写的服务封装器,很容易结合SHELL脚本编写守护脚本.运行后以独立进程存在.


快速安装:

yum -y install gcc autoconf
cd /xm-workspace/xm-webs/xmcloud/tomcat8081/bin
tar -zxvf commons-daemon-native.tar.gz  && cd commons-daemon-1.0.15-native-src/unix/
bash support/buildconf.sh
./configure --with-java=/xm-workspace/xm-webs/xmcloud/jdk && make
# bin目录下默认自带daemon.sh调用的就是jsvc命令
mv -f jsvc /xm-workspace/xm-webs/xmcloud/tomcat8081/bin/
cd ../../ && rm -rf commons-daemon-1.0.15-native-src



守护脚本:

#!/bin/sh  
# tomcat       run/start/stop/restart/version tomcat 
#  
# chkconfig:   2345 85 15  
# description: apache tomcat daemon script
#              run/start/stop/restart/version tomcat using jsvc.
  
# Source function library.
. /etc/init.d/functions
PORT=8081
HOME=/xm-workspace/xm-webs/xmcloud
TOMCAT_USER=tomcat
JAVA_HOME=${HOME}/jdk/jre
CATALINA_HOME=${HOME}/tomcat${PORT}
CATALINA_PID=/var/run/tomcat${PORT}.pid
JAVA_OPTS="-server -Xms1024m -Xmx1024m -Xmn384m -XX:PermSize=64m -XX:MaxPermSize=128m -XX:+UseParallelOldGC -XX:+PrintGCDateStamps -XX:+PrintGCDetails -Xloggc:${HOME}/tomcat${PORT}/logs/gc.log -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${HOME}/tomcat${PORT}/heapdump.bin"
export JAVA_HOME CATALINA_HOME CATALINA_PID TOMCAT_USER JAVA_OPTS
start(){
    ${CATALINA_HOME}/bin/daemon.sh start
    prog_ppid=`(cat ${CATALINA_PID}|grep -o "[^ ]\+\( \+[^ ]\+\)*") 2>/dev/null`
    echo -e "Host: $(hostname) Pid: ${prog_ppid:-"None"} Prog: ${CATALINA_HOME} Status: start"
}
stop(){
    prog_ppid=`(cat ${CATALINA_PID}|grep -o "[^ ]\+\( \+[^ ]\+\)*") 2>/dev/null`
    ${CATALINA_HOME}/bin/daemon.sh stop
    echo -e "Host: $(hostname) Pid: ${prog_ppid:-"None"} Prog: ${CATALINA_HOME} Status: stop"
}
status(){
    prog_ppid=`(cat ${CATALINA_PID}|grep -o "[^ ]\+\( \+[^ ]\+\)*") 2>/dev/null`
    if [[ "X${prog_ppid}" == "X" ]];then
        echo -e "Host: $(hostname) Pid: ${prog_ppid:-"None"} Prog: ${CATALINA_HOME} Status: stop"
    else
        echo -e "Host: $(hostname) Pid: ${prog_ppid:-"None"} Prog: ${CATALINA_HOME} Status: start"
    fi
}
version(){
    ${CATALINA_HOME}/bin/daemon.sh version
}
case "$1" in
    start)
        start
        exit $?
        ;;
    stop)
        stop
        exit $?
        ;;
 restart)
stop
start
exit $?
;;
  status)
        status
        exit $?
        ;;
 version)
        version
exit $?
;;
       *)
echo "Usage: tomcat${PORT} {start|stop|restart|status|version}" 
        exit $? 
esac
exit 1


访问测试:

说明: 测试前修改/xm-workspace/xm-webs/xmcloud/tomcat8081/conf/server.xml中将Connector的port值改为80,尝试/etc/init.d/tomcat8081 restart重启访问测试

开发利器_Jsvc.利用Jsvc实现Tomcat以普通用户运行监听80端口?_第1张图片