目的:实现 tomcat 开机非root账号的自动启动
系统环境:Ceotns 6.4 x64 ,TOMCAT 7.0.39 二进制发行包
一、环境准备
1.添加系统tomcat账号
[root@t246 ~]# groupadd tomcat [root@t246 ~]# useradd -g tomcat -s /sbin/nologin tomcat
2.安装ORACLE JDK (过程 略)
3.部署tomcat
[root@t246 java]# pwd /usr/java [root@t246 java]# ln -s apache-tomcat-7.0.39/ tomcat [root@t246 java]# chown -R tomcat:tomcat tomcat/
二、编译 jsvc
需要gcc编译器,没有的记得先yum装一下
[root@t246 bin]# cd /usr/java/tomcat/bin [root@t246 bin]# tar zxvf commons-daemon-native.tar.gz [root@t246 bin]# cd commons-daemon-1.0.14-native-src/unix [root@t246 unix]# ./configure [root@t246 unix]# make [root@t246 unix]# cp jsvc ../..
三、修改daemon.sh脚本
#!/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. # # ----------------------------------------------------------------------------- # Commons Daemon wrapper script. # # $Id: daemon.sh 1202058 2011-11-15 06:37:12Z mturk $ # ----------------------------------------------------------------------------- # #ADD for jsvc 1.0.14 段错误 if [ -z $LD_LIBRARY_PATH ]; then export LD_LIBRARY_PATH=/usr/local/lib:/usr/local/lib64 fi JAVA_HOME=/usr/java/default #添加 CATALINA_HOME=/usr/java/tomcat #添加 CATALINA_BASE=/usr/java/tomcat #添加 DAEMON_HOME=/usr/java/tomcat/bin #添加 TOMCAT_USER=tomcat #添加 # resolve links - $0 may be a softlink ARG0="$0" while [ -h "$ARG0" ]; do ls=`ls -ld "$ARG0"` link=`expr "$ls" : '.*-> \(.*\)$'` if expr "$link" : '/.*' > /dev/null; then ARG0="$link" else 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 以下省略
增加注释为“添加”的换变量
四、修改启动脚本
现在tomcat的二进制发行包里面有系统服务启动脚本的模版了,需要自己编写
[root@t246 ~]# touch /etc/init.d/tomcat [root@t246 ~]# chmod +x /etc/init.d/tomcat
脚本内容如下:(如果有mysql的话,注意启动顺序要在mysql之后)
[root@t246 ~]# cat /etc/init.d/tomcat #!/bin/bash # # Tomcat daemon.sh Startup script for the Tomcat Server # # chkconfig: 235 98 98 # description: Tomcat 7 server # processname: java # Source function library. . /etc/rc.d/init.d/functions #Tomcat Daemon path daemon_path=/usr/java/tomcat/bin # See how we were called. case "$1" in run) echo "Start Tomcat without detaching from console..." $daemon_path/daemon.sh run ;; start) echo "Start Tomcat..." $daemon_path/daemon.sh start ;; stop) echo "Stop Tomcat..." $daemon_path/daemon.sh stop ;; version) echo "What version of commons daemon and Tomcat are you running?" $daemon_path/daemon.sh version ;; *) echo "Usage: $0 {run|start|stop|version}" exit 1 ;; esac exit 0
添加服务
[root@t246 ~]# chkconfig --add tomcat