tomcat | Tianlin
1
2
3
4
5
# yum install apr apr-devel apr-utils openssl-devel
# tar xf tomcat-native-1.2.23-src.tar.gz
# cd tomcat-native-1.2.23-src/native/
# ./configure --with-apr=/usr/bin/apr-1-config --with-java-home=/usr/local/jdk --prefix=/usr/local/tomcat
# make && make install

将jdk和tomcat写入环境变量

1
2
3
4
5
# vim /etc/profile
export JAVA_HOME=/usr/local/jdk
export JRE_HOME=/usr/local/jdk/jre
export TOMCAT_HOME=/usr/local/tomcat
export PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin:$PATH

可以自己写一个小脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# /usr/lib/systemd/system/tomcat.service
[Unit]
Description=Unit Tomcat By Tianlin
Documentation=https://linjiangyu.com/
After=network-online.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
Environment=JAVA_HOME=/usr/local/jdk
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh

[Install]
WantedBy=multi-user.target
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
59
60
61
62
63
64
# vim /etc/init.d/tomcat
#!/bin/bash
#chkconfig: 2345 85 15
#description: By K <linjiangyu0702@qq.com>
# author: linjiangyu
# cc: https://creativecommons.org/licenses/by-nc-sa/4.0/
function state() {
netstat -tnlp | grep 8005 &> /dev/null
if [ $? -ne 0 ];then
let nu++
if [ $nu -eq 1 ];then
echo -e "\033[33mwait check listen 8005 listen health...\033[0m"
fi
if [[ $nu -ge 1 && $nu -lt 10 ]];then
sleep 1
state
else
echo -e "\033[31mbind listen 8005 not found\033[0m"
exit 1
fi
fi
}
case $1 in
start)
/usr/local/tomcat/bin/startup.sh
;;
restart)
# netstat -tnlp | grep 8005 &> /dev/null
nu=0
state
# while [ $? -ne 0 ]
# do
# let nu++
# if [ $nu -eq 1 ];then
# echo -e "\033[33mwait check server health\033[0m"
# fi
# sleep 1
# netstat -tnlp | grep 8005 &> /dev/null
# done
/usr/local/tomcat/bin/shutdown.sh &> /dev/null
[ $? -eq 0 ] && /usr/local/tomcat/bin/startup.sh | tail -1 || echo -e "\033[31m$(basename $0) start is failed\033[0m"
;;
stop)
nu=0
state
/usr/local/tomcat/bin/shutdown.sh
kill $(ps aux | grep tomcat | grep -v grep | awk '{ print $2 }')
;;
status)
/usr/local/tomcat/bin/configtest.sh
;;
*)
echo -e "\033[31mUsage: $(basename $0) (start|stop|status)\033[0m"
;;
esac
<!--这里要吐槽一下java的程序运行的是真的慢,服务刚启动的话8005的套接字端口不会那么快就启用,而关闭服务的时候是要使用到8005端口shutdown的,所以应该把restart服务加上一段时间-->

# chmod +x /etc/init.d/tomcat
# vim /usr/local/tomcat/bin/catalina.sh # 把环境变量写入相应需要的文件中(tomcat的变量这里没有引用系统环境变量,所以要自己再写上)
export JAVA_HOME=/usr/local/jdk
export JRE_HOME=/usr/local/jdk/jre
# chkconfig --add tomcat
# chkconfig tomcat on
# service tomcat start
  • unit
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
# cat > /usr/lib/systemd/system/tomcat.service <<END
# /usr/lib/systemd/system/tomcat.service
# author: linjiangyu
# cc: https://creativecommons.org/licenses/by-nc-sa/4.0/

[Unit]
Documentation=man:systemd-sysv-generator(8)
SourcePath=/etc/rc.d/init.d/tomcat
Description=SYSV: By K <linjiangyu0702@qq.com>
Before=runlevel2.target
Before=runlevel3.target
Before=runlevel4.target
Before=runlevel5.target
Before=shutdown.target
After=network-online.target
After=network.service
Conflicts=shutdown.target

[Service]
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
ExecStart=/etc/rc.d/init.d/tomcat start
ExecStop=/etc/rc.d/init.d/tomcat stop
END
# systemctl daemon-reload
# systemctl enable --now tomcat
  • service(未完全测试)
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
# cat > /etc/init.d/tomcat <<END
#!/bin/bash
#chkconfig: 2345 85 15
#description: By K <linjiangyu0702@qq.com>
#author: linjiangyu
#cc: https://creativecommons.org/licenses/by-nc-sa/4.0/
function state() {
netstat -tnlp | grep 8005 &> /dev/null
if [ $? -ne 0 ];then
let nu++
if [ $nu -eq 1 ];then
echo -e "\033[33mwait check listen 8005 listen health...\033[0m"
fi
if [[ $nu -ge 1 && $nu -lt 10 ]];then
sleep 1
state
else
echo -e "\033[31mbind listen 8005 not found\033[0m"
exit 1
fi
fi
}
case $1 in
start)
/usr/local/tomcat/bin/startup.sh
;;
restart)
# netstat -tnlp | grep 8005 &> /dev/null
nu=0
state
# while [ $? -ne 0 ]
# do
# let nu++
# if [ $nu -eq 1 ];then
# echo -e "\033[33mwait check server health\033[0m"
# fi
# sleep 1
# netstat -tnlp | grep 8005 &> /dev/null
# done
/usr/local/tomcat/bin/shutdown.sh &> /dev/null
[ $? -eq 0 ] && /usr/local/tomcat/bin/startup.sh | tail -1 || echo -e "\033[31m$(basename $0) start is failed\033[0m"
;;
stop)
nu=0
state
/usr/local/tomcat/bin/shutdown.sh
;;
status)
/usr/local/tomcat/bin/configtest.sh
;;
*)
echo -e "\033[31mUsage: $(basename $0) (start|stop|status)\033[0m"
;;
esac
END

# chmod u+x /etc/init.d/tomcat
  • Unit
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
# /usr/lib/systemd/system/tomcat.service
# author: linjiangyu
# cc: https://creativecommons.org/licenses/by-nc-sa/4.0/
[Unit]
Documentation=https://linjiangyu.com/tomcat
SourcePath=/etc/rc.d/init.d/tomcat
Description=SYSV: By K <linjiangyu0702@qq.com>
Before=runlevel2.target
Before=runlevel3.target
Before=runlevel4.target
Before=runlevel5.target
Before=shutdown.target
After=network-online.target
After=network.service
Conflicts=shutdown.target

[Service]
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
ExecStart=/etc/rc.d/init.d/tomcat start
ExecStop=/etc/rc.d/init.d/tomcat stop

[Install]
WantedBy=multi-user.target
avatar
🐟认真摸鱼中
Tianlin
风有约 花不误
Hearken
公告栏
--- 主域名 ---
linjiangyu.com 🍧
--- 私人云盘 ---
alist.linjiangyu.com📮
--- 备用域名 ---
fleek.linjiangyu.com🍰
netlify.linjiangyu.com🍨
cloudflare.linjiangyu.com🍬
微博热搜
空降评论复制本文地址
随便逛逛昼夜切换关于博客美化设置切换全屏打印页面