Publish linux services with jsvc

Download commons-daemon-native.tar.gz, which can be obtained from the bin directory of TomcatX.

tar zxvf commons-daemon-native.tar.gz
cd commons-daemon-1.0.13-native-src/unix/

If it is a 64-bit system, you need to modify jsvc-unix.c under native

Modify the declaration of the char array of libcap_locs, otherwise the error that libcap.so cannot be found will be reported in the debug information

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
};

 Compile and install

./configure
make
cp jsvc /data0/tomcat/bin

 At this point, the jsvc installation is over.

 Installation can refer to http://commons.apache.org/daemon/jsvc.html

 

  Let's start making the service of the linux system

   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"
     be
}

case "$1" in
  start)
    start
    ;;

  stop)
    stop
    ;;
  restart|reload)
        stop
        start
        RETVAL=$?
        ;;
  status)
        status
        RETVAL=$?
        ;;
  *)
    echo "Usage tomcat.sh start/stop"
    exit 1;;
esac

Add the service below

chkconfig --add tomcat
chkconfig --list|grep tomcat

 debugging

[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

 Under Centos, if you can see the two processes of father and son, it will be right.

 If you have permission problems, please 755 or chown

 I hope it will be helpful to my friends, which can solve the problem of Tomcat's startup user permissions and avoid switching users back and forth.

 If you have a better way, welcome everyone to discuss and exchange.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327056560&siteId=291194637