2013年8月9日星期五

linux as a daemon process is running under tomcat

 
  If you want tomcat under linux at boot time from the start, you can start writing code to / etc / rc.local inside. However, in this case, tomcat will be run with root privileges, which is unsafe. Therefore, finding ways to allow non-privileged identity tomcat run as a daemon.  
 
    
 
  To tomcat as a daemon running linux, you need commons-daemon project jsvc tool, tomcat's bin directory already comes with this tool source.  
 
    
 
  Unzip commons-daemon-native.tar.gz, enter the unix directory, and then configure. configure, you need to specify the jdk path or in the current environment there JAVA_HOME variable.  
 
    
 
  The next time the make the following error may occur:  
 
  
   
    
ar: libservice.a: Malformed archive 
make
[1]: *** [libservice.a] Error 1
   
  
 
 
  This is a known bug:  
 
  
   
    
The file bin/commons-daemon-native.tar.gz contains dirty (already compiled) 
code. On some systems this causes
"make" to return the following error:
ar: libservice.a: Malformed archive
The solution is to run
"make clean" before running "make".
I believe that
"make clean" should be run before creating the tgz file, so that
there are no compiled/generated files laying around.
   
  
 
 
  make later get jsvc file to the tomcat's bin directory. Also in the native directory has a Tomcat5.sh, for tomcat since the launch of a template, we can modify it to meet the requirements to get a quick start-up files. Here, I will change it to the following:  
 
  
   
    
#!/bin/sh 
##############################################################################
#
# Licensed to the Apache Software Foundation (ASF) under one or
more
# contributor license agreements. See the NOTICE
file distributed with
# this work
for additional information regarding copyright ownership.
# The ASF licenses this
file to You under the Apache License, Version 2.0
# (the
"License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http:
//www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to
in writing, software
# distributed under the License is distributed on an
"AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License
for the specific language governing permissions and
# limitations under the License.
##############################################################################
#
# Small shell script to show how to start
/stop Tomcat using jsvc
# If you want to have Tomcat running on port
80 please modify the server.xml
#
file:
#
#
<!-- Define a non-SSL HTTP/1.1 Connector on port 80 -->
#
<Connector className="org.apache.catalina.connector.http.HttpConnector"
# port
="80" minProcessors="5" maxProcessors="75"
# enableLookups
="true" redirectPort="8443"
# acceptCount
="10" debug="0" connectionTimeout="60000"/>
#
# That is
for Tomcat-5.0.x (Apache Tomcat/5.0)
#
# Adapt the following lines to your configuration
JAVA_HOME
=/usr/local/sun-java6-jdk
CATALINA_HOME
=/usr/local/apache-tomcat-6
DAEMON_HOME
=$CATALINA_HOME
TOMCAT_USER
=tomcat6

#
for multi instances adapt those lines.
TMP_DIR
=/var/tmp
PID_FILE
=/var/run/jsvc.pid
CATALINA_BASE
=$CATALINA_HOME

#CATALINA_OPTS
="-Djava.library.path=/home/jfclere/jakarta-tomcat-connectors/jni/native/.libs"
CLASSPATH
=/
$JAVA_HOME
/lib/tools.jar:/
$CATALINA_HOME
/bin/commons-daemon.jar:/
$CATALINA_HOME
/bin/bootstrap.jar

case "$1" in
start)
#
# Start Tomcat
#
echo "Starting tomcat6..."
$DAEMON_HOME
/bin/jsvc /
-user $TOMCAT_USER /
-home $JAVA_HOME /
-Dcatalina.home=$CATALINA_HOME /
-Dcatalina.base=$CATALINA_BASE /
-Djava.io.tmpdir=$TMP_DIR /
-wait 10 /
-pidfile $PID_FILE /
-outfile $CATALINA_HOME/logs/catalina.out /
-errfile $CATALINA_HOME/logs/catalina.err /
$CATALINA_OPTS
/
-cp $CLASSPATH /
org.apache.catalina.startup.Bootstrap
#
# To get a verbose JVM
#
-verbose /
# To get a debug of jsvc.
#
-debug /
if test $? -eq 0
then
exit
0
else
echo "Failed to start tomcat6"
exit
1
fi
;;

stop)
#
# Stop Tomcat
#
$DAEMON_HOME
/bin/jsvc /
-stop /
-pidfile $PID_FILE /
org.apache.catalina.startup.Bootstrap
if test $? -eq 0
then
echo "tomcat6 stopped"
exit
0
else
echo "Failed to stop tomcat6"
exit
1
fi
;;

restart)
#
# Restart Tomcat
#
if $0 stop
then
$
0 start
else
echo "Failed to stop running server, so refusing to try to start."
fi
exit
0
;;


*)
echo "Usage: tomcat6 start|stop|restart"
exit
1;;
esac
   
In the script above which specifies TOMCAT_USER, jsvc privileged identity will be the first to start tomcat, then switch to the specified user, this could make tomcat as a non-privileged listeners need privileged ports.   
 
 
    
 
  Added to the system services (for example here in Debian), copy the script to / etc / init.d, renamed tomcat6, and then perform  
 
  
   
    
update-rc.d tomcat6 defaults
   
After configuration is complete, tomcat can with the system from the start, and as a non-privileged user daemon running.   
  
      
 

没有评论:

发表评论