![在 Linux 中启动 apache tomcat](https://linux22.com/image/1343089/%E5%9C%A8%20Linux%20%E4%B8%AD%E5%90%AF%E5%8A%A8%20apache%20tomcat.png)
我想知道是否有任何方法可以让我们的 Apache tomcat 在启动时自动运行?我想简化我们的工作,因为我们不想在每次重新启动 Linux 机器时执行启动脚本(在 Apache tomcat 目录中提供)。希望有人能帮忙。谢谢。
答案1
您只需将启动脚本添加到您的启动顺序中。
最简单的方法(我认为)是在 /etc/init.d 中创建一个启动脚本,并将其添加到您想要的运行级别。这是我使用的脚本(后来我假设该脚本名为 tomcat)。
#!/bin/sh
#
# tomcat7 This shell script takes care of starting and stopping Tomcat
#
# chkconfig: - 80 20
#
### BEGIN INIT INFO
# Provides: tomcat7
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start:
# Default-Stop:
# Description: Release implementation for Servlet 2.5 and JSP 2.1
# Short-Description: start and stop tomcat
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
START_DAEMON=/opt/apache-tomcat-6.0.33/bin/startup.sh
STOP_DAEMON=/opt/apache-tomcat-6.0.33/bin/shutdown.sh
NAME=tomcat
DESC=tomcat
case "$1" in
start)
tomcat_pid=`ps -ef | grep apache | grep tomcat | grep java | egrep -v grep | awk '{print $2}'`
if [ -n "$tomcat_pid" ]; then
echo "Tomcat is running."
exit 1;
fi
echo -n "Starting $DESC:\n $START_DAEMON \n"
$START_DAEMON
;;
stop)
tomcat_pid=`ps -ef | grep apache | grep tomcat | grep java | egrep -v grep | awk '{print $2}'`
if [ -z "$tomcat_pid" ]; then
echo "Tomcat is not running."
exit 1;
fi
echo -n "Stopping $DESC:\n $STOP_DAEMON \n "
$STOP_DAEMON
;;
restart|force-reload)
tomcat_pid=`ps -ef | grep apache | grep tomcat | grep java | egrep -v grep | awk '{print $2}'`
if [ -n "$tomcat_pid" ]; then
echo -n "Restarting $DESC: \n $STOP_DAEMON \n"
$STOP_DAEMON
sleep 5
fi
echo -n "starting $DESC: \n $START_DAEMON \n"
$START_DAEMON
echo "Starting, please wait for about 50 seconds."
sleep 20
;;
*)
echo "Usage: $NAME {start|stop|restart}" >&2
exit 1
;;
esac
exit 0
现在将它添加到启动取决于你的发行版。例如:Ubuntu 的默认运行级别看起来就像sudo update-rc.d tomcat defaults
Red Hat 看起来chkconfig --add tomcat
一样chkconfig tomcat on