在linux下如果想让tomcat在开机时自启动,可以将启动代码写到/etc/rc.local里面。但是,这样的话,tomcat将以root权限运行,这是不安全的。因此,要想办法让tomcat以非特权身份作为daemon运行。
要将tomcat作为linux的daemon运行,需要commons-daemon工程的jsvc工具,tomcat的bin目录里已经自带了这个工具的源码。
解压commons-daemon-native.tar.gz,进入unix子目录,然后configure。configure的时候需要指定jdk路径或者当前环境中有JAVA_HOME变量。
接下来在make的时候可能会出现如下错误:
ar: libservice.a: Malformed archive make[1]: *** [libservice.a] Error 1
这是一个已知的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以后得到jsvc文件,复制到tomcat的bin目录中。另外在native目录下有一个Tomcat5.sh,是用于tomcat自启动的一个模板,我们可以修改它快速得到一个符合要求的启动文件。在这里,我将它修改为如下内容:
#!/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
在以的脚本里面指定了TOMCAT_USER,jsvc将先以特权身份启动tomcat,随后切换到指定的用户,这样可以使tomcat以非特权身份监听需要特权的端口。
将脚本复制到/etc/init.d,改名为tomcat6,然后执行
update-rc.d tomcat6 defaults
配置完成以后,tomcat就可以以非特权用户作为daemon运行了。
相关推荐
Daemon程序简介Daemon是长时间运行的进程,通常在系统启动后就运行,在系统关闭时才结束。一般说Daemon程序在后台运行,是因为它没有控制终端,无法和...本文介绍了在Linux下编写Daemon程序的步骤,并给出了例子程序。
# 前言:为什么要使用非root用户运行tomcat root用户启动tomcat有一个严重的问题,那就是tomcat具有root权限。 这意味着你的任何一个页面脚本(html/js)都具有root权限,所以可以轻易地用页面脚本 修改整个硬盘里的...
在Linux下用QT开发Daemon程序.pdf
Framework for running BPF programs with rules on Linux as a daemon. Container aware.
ssh整合的时候 启动tomcat常见的错误
根据《linux/UNIX系统编程手册》的daemon章节写的一个测试become_daemon()函数程序。代码内容99%以上都是书上给的源码。此处仅注释掉不需要的内容,以及为了简化理解,做了不同平台的适配,(对于源码来说是种倒退,...
在Linux中专门提供了一... 经过这个函数调用后的程序将运行在后台,成为一个daemon程序,而linux下大多的服务都是以此方式运行的。 我们来看一个简单的例子。例如编写例子程序test.c #include #include int d
2,TOMCAT目录结构 3,TOMCAT端口管理 4,TOMCAT账号管理 5,TOMCAT配置数据库 6,TOMCAT监控软件安装 7,TOMCAT环境变量 8,TOMCAT和JVM的配置 9,TOMCAT基于名称的虚拟主机 10,TOMCAT IP访问控制 11,TOMCAT...
linux daemon and zombie process
在Linux系统下配置service启动和关闭 1, 通过命令cd /etc/init.d文件夹下 2, 再通过命令 vim tomcat 进入vim编辑界面 3,用过 i键 现在把下面代码贴入编辑界面 shell脚本如下 #!/bin/bash # This is the ...
一个简单的linux 守护进程例子, 适合linux初学者做参考
daemon.c 文件 在linux下直接编译运行 即可
linux的daemon监视程序,当服务daemon不正常退出时能自动重新启动,保证关键服务的高可用性,比如Apache服务器。
创建一个新服务的监控非常简单,创建一个目录,并且在此目录下写一个名称为'run'的脚本,在这个脚本里定义启动目标服务的命令即可。 4 multilog将错误信息保存到一个或多个日志中。It optionally timestamps each ...
Linux-Daemon_Writting.pdf
Daemon_Tools Daemon_Tools Daemon_Tools Daemon_Tools
通过Apache Commons Daemon实现jar包部署成windows系统服务
虚拟光驱 daemon_tools_347cn_eric虚拟光驱 daemon_tools_347cn_eric虚拟光驱 daemon_tools_347cn_eric虚拟光驱 daemon_tools_347cn_eric虚拟光驱 daemon_tools_347cn_eric虚拟光驱 daemon_tools_347cn_eric虚拟光驱...
Daemon Tools v4.03 V4.03HE 汉化版,相信经常使用虚拟光驱的人都应该记得,这是当年极其经典的一个版本。 现在的虚拟光驱越做越大,功能也越来越...XP系统正常运行,WIN7我没有试过。 这是非常好用的一个虚拟光驱!
daemon 程序例子daemon 程序例子daemon 程序例子