下载commons-daemon-native.tar.gz,可以从TomcatX的bin目录下获得。
tar zxvf commons-daemon-native.tar.gz
cd commons-daemon-1.0.13-native-src/unix/
如果是64位系统,需要修改native下的jsvc-unix.c
修改libcap_locs这个char数组的声明,不然在debug信息里会报找不到libcap.so的错误
static const char *libcap_locs[] = {
"/lib64/libcap.so.2",
"/lib64/libcap.so.1",
"/lib64/libcap.so",
"/lib/libcap.so.2",
"/lib/libcap.so.1",
"/lib/libcap.so",
"/usr/lib64/libcap.so.2",
"/usr/lib64/libcap.so.1",
"/usr/lib64/libcap.so",
"/usr/lib/libcap.so.2",
"/usr/lib/libcap.so.1",
"/usr/lib/libcap.so",
NULL
};
编译安装
./configure
make
cp jsvc /data0/tomcat/bin
至此,jsvc安装结束了。
安装可参考http://commons.apache.org/daemon/jsvc.html
下面开始制作linux系统的service
vi /etc/init.d/tomcat
#!/bin/sh
# Adapt the following lines to your configuration
. /etc/rc.d/init.d/functions
prog=tomcat
JAVA_HOME=/data1/software/jdk/jdk1.6.0_33
CATALINA_HOME=/data0/tomcat
DAEMON_HOME=/data0/tomcat/bin
TOMCAT_USER=adserver
# for multi instances adapt those lines.
TMP_DIR=/var/tmp
PID_FILE=/var/run/jsvc.pid
CATALINA_BASE=/data0/tomcat
CATALINA_OPTS=
CLASSPATH=\
$JAVA_HOME/lib/tools.jar:\
$CATALINA_HOME/bin/commons-daemon.jar:\
$CATALINA_HOME/bin/bootstrap.jar:\
$CATALINA_HOME/bin/tomcat-juli.jar
start(){
#
# Start Tomcat
#
$DAEMON_HOME/jsvc \
-user $TOMCAT_USER \
-home $JAVA_HOME \
-Dcatalina.home=$CATALINA_HOME \
-Dcatalina.base=$CATALINA_BASE \
-Djava.io.tmpdir=$TMP_DIR \
-pidfile $PID_FILE \
-outfile $CATALINA_HOME/logs/catalina.out \
-errfile $CATALINA_HOME/logs/catalina.err \
-cp $CLASSPATH \
org.apache.catalina.startup.Bootstrap
RETVAL=$?
[ $RETVAL = 0 ] && touch /var/lock/subsys/jsvc
[ $RETVAL = 0 ] && echo_success || echo_failure
echo $"Starting $prog: "
return $RETVAL
}
stop(){
#
# Stop Tomcat
#
$DAEMON_HOME/jsvc \
-stop \
-pidfile $PID_FILE \
org.apache.catalina.startup.Bootstrap
RETVAL=$?
[ $RETVAL = 0 ] && rm /var/lock/subsys/jsvc
[ $RETVAL = 0 ] && echo_success || echo_failure
echo $"Stopping $prog: "
return $RETVAL
}
status() {
ps ax --width=1000 | grep "[o]rg.apache.catalina.startup.Bootstrap" | awk '{printf $1 " "}' | wc | awk '{print $2}' >/tmp/tomcat_process_count.txt
read line < /tmp/tomcat_process_count.txt
if [ $line -gt 0 ]; then
echo -n "Tomcat ( pid "
ps ax --width=1000 | grep "[o]rg.apache.catalina.startup.Bootstrap" | awk '{printf $1 " "}'
echo ") is running "
else
echo "Tomcat is stopped"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload)
stop
start
RETVAL=$?
;;
status)
status
RETVAL=$?
;;
*)
echo "Usage tomcat.sh start/stop"
exit 1;;
esac
下面添加服务service
chkconfig --add tomcat
chkconfig --list|grep tomcat
调试
[root@gina53 unix]# service tomcat start
Starting tomcat: [ OK ]
[root@gina53 unix]# service tomcat status
Tomcat ( pid 23259 23260 ) is running
[root@gina53 unix]# ps -ef|grep tomcat
root 23259 1 0 16:33 ? 00:00:00 jsvc.exec -user adserver -home /data1/software/jdk/jdk1.6.0_33 -Dcatalina.home=/data0/tomcat -Dcatalina.base=/data0/tomcat -Djava.io.tmpdir=/var/tmp -pidfile /var/run/jsvc.pid -outfile /data0/tomcat/logs/catalina.out -errfile /data0/tomcat/logs/catalina.err -cp /data1/software/jdk/jdk1.6.0_33/lib/tools.jar:/data0/tomcat/bin/commons-daemon.jar:/data0/tomcat/bin/bootstrap.jar:/data0/tomcat/bin/tomcat-juli.jar org.apache.catalina.startup.Bootstrap
adserver 23260 23259 99 16:33 ? 00:00:38 jsvc.exec -user adserver -home /data1/software/jdk/jdk1.6.0_33 -Dcatalina.home=/data0/tomcat -Dcatalina.base=/data0/tomcat -Djava.io.tmpdir=/var/tmp -pidfile /var/run/jsvc.pid -outfile /data0/tomcat/logs/catalina.out -errfile /data0/tomcat/logs/catalina.err -cp /data1/software/jdk/jdk1.6.0_33/lib/tools.jar:/data0/tomcat/bin/commons-daemon.jar:/data0/tomcat/bin/bootstrap.jar:/data0/tomcat/bin/tomcat-juli.jar org.apache.catalina.startup.Bootstrap
Centos下如果能看到父子2个进程就对了。
若有权限问题,请755或者chown
希望对朋友们有所帮助,这样可以解决Tomcat的启动用户权限问题,避免来回切换用户。
如果你有更好的方法,欢迎大家一起讨论交流。