在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[]: *** [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 please modify the server.xml
# file:
#
# <!-- Define a non-SSL HTTP/1.1 Connector on port -->
# <Connector className="org.apache.catalina.connector.http.HttpConnector"
# port="" minProcessors="" maxProcessors=""
# enableLookups="true" redirectPort=""
# acceptCount="" debug="" connectionTimeout=""/>
#
# 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-
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 /
-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
then
exit
else
echo "Failed to start tomcat6"
exit
fi
;; stop)
#
# Stop Tomcat
#
$DAEMON_HOME/bin/jsvc /
-stop /
-pidfile $PID_FILE /
org.apache.catalina.startup.Bootstrap
if test $? -eq
then
echo "tomcat6 stopped"
exit
else
echo "Failed to stop tomcat6"
exit
fi
;; restart)
#
# Restart Tomcat
#
if $ stop
then
$ start
else
echo "Failed to stop running server, so refusing to try to start."
fi
exit
;; *)
echo "Usage: tomcat6 start|stop|restart"
exit ;;
esac

在以上脚本里面指定了TOMCAT_USER,jsvc将先以特权身份启动tomcat,随后切换到指定的用户,这样可以使tomcat以非特权身份监听需要特权的端口。

 
加入系统服务(这里以Debian为例),将脚本复制到/etc/init.d,改名为tomcat6,然后执行
update-rc.d tomcat6 defaults

配置完成以后,tomcat就可以随系统自启动,并且以非特权用户作为daemon运行了。

 

linux下tomcat作为daemon进程运行的更多相关文章

  1. Linux下tomcat作为守护进程运行(开机启动、以指定的用户运行、解决非root身份不能绑定1024以下端口的问题)的配置方法

    如题. 参考资料: http://www.jdiy.org/read.jd?id=y0haaynq1w http://blog.csdn.net/shw2004/article/details/578 ...

  2. Linux下Tomcat端口、进程以及防火墙设置

     Linux下Tomcat端口.进程以及防火墙设置 1,查看tomcat进程: #ps -aux | grep tomcat(或者ps -ef | grep tomcat都行) 可以看到现在运行着两个 ...

  3. 解决linux下tomcat停止进程任存在问题

    解决linux下tomcat停止进程任存在问题 在Linux下(之所以强调linux下,是因为在windows下正常),执行tomcat ./shutdown.sh 后,虽然tomcat服务不能正常访 ...

  4. Linux 下Tomcat的启动、关闭、杀死进程

    Linux下Tomcat的启动.关闭.杀死进程 打开终端 cd /java/tomcat #执行 bin/startup.sh #启动tomcat bin/shutdown.sh #停止tomcat ...

  5. linux 下 tomcat 运行报错 Broken pipe

    linux 下 tomcat 运行报错 Broken pipe 感谢:http://hi.baidu.com/liupenglover/blog/item/4048c23ff19f1cd67d1e71 ...

  6. Linux下tomcat的shutdown命令可以关闭服务但是杀不死进程

    Linux下tomcat的shutdown命令可以关闭服务但是杀不死进程 原因: 一般造成这种原因是因为项目中有非守护线程的存在: 解决方案: 一.从Tomcat上解决 方案1:(推荐的方案:因为一台 ...

  7. 阿里云OneinStack,Linux下tomcat命令

    阿里云OneinStack,Linux下tomcat命令 Linux下如何查看tomcat是否启动在Linux系统下,重启Tomcat使用命令操作的首先,进入Tomcat下的bin目录cd /usr/ ...

  8. Linux下tomcat部署

    进入Tomcat下的bin目录 cd /usr/local/tomcat/bin 如果你想直接干掉Tomcat,你可以使用kill命令,直接杀死Tomcat进程 kill -9 7010 然后继续查看 ...

  9. Linux下tomcat服务

    一:Linux下tomcat服务的启动.关闭与错误跟踪,使用PuTTy远程连接到服务器以后,通常通过以下几种方式启动关闭tomcat服务:切换到tomcat主目录下的bin目录(cd usr/loca ...

随机推荐

  1. MS MDS系列之MDS层次结构(Hierarchy)

    在Master Data Services中,Hierarchy的作用主要用于: 对同属性成员进行分组 聚合成员用于分析和报告输出 写在开始:显示层次结构(Explicit Hierarchy)即将在 ...

  2. linux服务器部署jar包以及shell脚本的书写

    背景:记录在linux环境下部署jar程序的过程 1 部署过程记录 1.1 程序结构 这里的main函数就在DemRest2.java 文件中. 为了部署方便,要做到以下两点: 1 在导出的jar包中 ...

  3. Windows查看端口使用状况

    使用端口是我们在进行远程或者打印机等都会遇到的,但是有很多用户会遇到端口被占用的情况,遇到这样的问题首先就要找出电脑中的所以端口然后进行查看,还是有很多人不知道该如何查看电脑端口. 1 查看windo ...

  4. Android学习笔记-绘制圆形ImageView实例

    现在很多的APP都很喜欢圆形的头像,这里就简单的写个圆形的ImageView~ 第三方圆形ImageView控件: RoundedImageView CircleImageView 实现代码: 自定义 ...

  5. css中的几个小tip(一)

    原博:http://www.webhek.com/post/7-tips-web-front-developer-must-know__trashed.html 1.元素的margin-top.mar ...

  6. [js高手之路] es6系列教程 - 箭头函数详解

    箭头函数是es6新增的非常有意思的特性,初次写起来,可能会觉得别扭,习惯之后,会发现很精简. 什么是箭头函数? 箭头函数是一种使用箭头( => )定义函数的新语法, 主要有以下特性: 不能通过n ...

  7. HDU6055 Regular polygon(计算几何)

    Description On a two-dimensional plane, give you n integer points. Your task is to figure out how ma ...

  8. 表达式求值(二叉树方法/C++语言描述)(四)

    代码清单 // binarytree.h #ifndef BINARYTREE_H #define BINARYTREE_H template <typename T> class Bin ...

  9. 1059. C语言竞赛

    C 语言竞赛是浙江大学计算机学院主持的一个欢乐的竞赛.既然竞赛主旨是为了好玩,颁奖规则也就制定得很滑稽: 冠军将赢得一份"神秘大奖"(比如很巨大的一本学生研究论文集--). 排名为 ...

  10. 在drawRect:方法中绘制图片,文字以及Core Graphics 框架的了解

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0. ...