---
title: "Linux下tomcat自启动脚本"
author: "justshare"
date: "2009-06-24T17:55:33Z"
tags: []
slug: "linux-tomcat"
draft: false
lastmod: "2009-06-24T17:56:37Z"
id: 414340
url: "https://justshare.iteye.com/blog/414340"
---

ZZ原文地址：[tomcat自启动脚本](http://www.iteye.com/topic/177981 "tomcat自启动脚本")

 

1、 创建服务名为 tomcat 的自动启动过程 中，将%tomcat\_home%/bin目录下的startup.sh启动文件COPY至/etc/rc.d/init.d目录下，并改名为：tomcat ，以后就可以使用service tomcat start/stop/status/restart等命令了

2、 在/etc/rc.d/rc3.d目录下，执行创建超链接 ln –s /etc/rc.d/init.d/tomcat  /etc/init.d  
这样，开机就会自动启动Tomcat了。

3.如果不能运行，请检查  
chmod 755 /etc/init.d/tomcat  
chmod 755 apache-tomcat-5.5.25/  
vi /etc/passwd  
修改启动用户如  
apache 的/sbin/nologin 为/bin/bash

4、 脚本  
#!/bin/sh  
\#  
\# Start staff.macaufly.net webserver  
\# Currently installed under /home/jira and should moved to /home/tomcat later  
\#

TOMCAT\_BIN=/home/tomcat6/bin  
TOMCAT\_USER=apache

start() {

stop  
su - $TOMCAT\_USER -c $TOMCAT\_BIN/startup.sh  
sleep 10  
if netstat -an | grep 8080 &gt;/dev/null  
then  
echo "Tomcat is running"  
return 0  
else  
return 1  
fi  
}

stop() {  
if netstat -an | grep 8080 | grep LISTEN &gt;/dev/null  
then  
\# try to shutdown the server first  
echo "Now Shutdown the server..."  
su - $TOMCAT\_USER -c  $TOMCAT\_BIN/shutdown.sh  
else  
echo "Tomcat is not running"  
return 0  
fi

if netstat -an | grep 8080 | grep LISTEN  
then  
sleep 10  
echo "Now kill it"  
ps -ef | grep java | grep $TOMCAT\_USER | awk '{print "kill -9 ", $2;}' | sh  
fi

if netstat -an | grep 8080 | grep LISTEN &gt;/dev/null  
then  
return 1 # still alive  
else  
echo "Tomcat is stopped"  
return 0  
fi  
}

case "$1" in  
  start)  
start  
;;

  stop)  
stop  
;;

  restart)  
start  
;;

  status)  
  if netstat -an |grep 8080|grep LISTEN &gt;/dev/null  
then  
echo "Tomcat is Running"  
else  
echo "Tomcat is Not Running"  
fi  
;;

  \*)  
echo $"Usage: $0 {start|stop|restart|status}"  
exit 1  
esac

exit 0

ZZ原文地址：[tomcat自启动脚本](http://www.iteye.com/topic/177981 "tomcat自启动脚本")