linux中设置tomcat开机启动

编写以下脚本放入/etc/init.d/目录.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# tomcat自启动脚本
#!/bin/sh
# chkconfig: 345 99 10
# description: Auto-starts tomcat
# /etc/init.d/tomcatd
# Tomcat auto-start
# Source function library.
#. /etc/init.d/functions
# source networking configuration.
#. /etc/sysconfig/network
RETVAL=0
export JDK_HOME=Your JDK Path
export CATALINA_HOME=Your Tomcat Path
export CATALINA_BASE=Your Tomcat Path
start()
{
if [ -f $CATALINA_HOME/bin/startup.sh ];
then
echo $"Starting Tomcat"
$CATALINA_HOME/bin/startup.sh
RETVAL=$?
echo " OK"
return $RETVAL
fi
}
stop()
{
if [ -f $CATALINA_HOME/bin/shutdown.sh ];
then
echo $"Stopping Tomcat"
$CATALINA_HOME/bin/shutdown.sh
RETVAL=$?
sleep 1
ps -fwwu tomcat | grep apache-tomcat|grep -v grep | grep -v PID | awk '{print $2}'|xargs kill -9
echo " OK"
# [ $RETVAL -eq 0 ] && rm -f /var/lock/...
return $RETVAL
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
echo $"Restaring Tomcat"
$0 stop
sleep 1
$0 start
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
;;
esacP
exit $RETVAL

完成后增加可执行权限:

1
chmod +x /etc/init.d/tomcat

再创建软连接到/etc/rc3.d/目录, rc3.d中的3表示开机启动级别, 3表示命令行模式启动.

1
ln -s /etc/init.d/tomcat /etc/rc3.d/S98tomcat

软连接名称S98tomcat中的S98表示启动顺序排在98号, 通过名称可控制服务启动顺序, 以保证一些有特定启动顺序的服务能正常启动.

最后将tomcat注册为服务, chkconfig --add tomcat. 搞定收工.

文章目录
,