Linux CentOS7 开机启动 Tomcat

一旦 Tomcat 安装完成并成功在运行,我们就可以设置在系统重启时自动启动 Tomcat。这可以确保每次启动系统时,Tomcat 就会自动运行并且处理请求。

注意:下面将在 CentOS7 中,通过编辑 rc.local 文件去实现开机后自动启动 Tomcat。

具体步骤

安装 Tomcat

确保已经成功安装 Tomcat,能够正常启动且访问。如何安装 Tomcat 请参考 “Linux 安装 Tomcat

创建启动脚本

直接在 Tomcat 主目录下面创建一个名为 start.sh 的脚本文件,后续会使用该文件。脚本文件内容如下:

#!/bin/sh
echo start tomcat > tmp.log
/root/tomcat-8.5.73/bin/startup.sh

上面脚本中,第二行主要用来验证在 CentOS7 重启后,是否真的执行了该脚本文件。第三行脚本通过调用 Tomcat 的 startup.sh 脚本文件去启动 Tomcat。

创建完启动脚本后,通过 chmod 给 start.sh 脚本授予可执行权限。命令如下:

[root@localhost tomcat-8.5.73]# chmod +x start.sh 
[root@localhost tomcat-8.5.73]# ll start.sh 
-rwxr-xr-x. 1 root root 74 Dec  6 23:20 start.sh

编辑 rc.local 文件

使用 root 权限进入到 /etc 目录,给 rc.local 文件添加可执行权限。命令如下:

[root@localhost etc]# chmod +x rc.local 
[root@localhost etc]# ll rc.local 
lrwxrwxrwx. 1 root root 13 Aug  9  2020 rc.local -> rc.d/rc.local

编辑 rc.local 文件,在文件末尾添加如下内容:

echo execute start.sh > /root/data.log
# start tomcat
/root/tomcat-8.5.73/start.sh

上面脚本中,第一行用来验证操作系统重启后,是否真的执行了 rc.local 文件。第三行脚本直接调用上面创建 start.sh 脚本,通过该脚本去启动 tomcat。完整的 rc.local 脚本内容如下:

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local

# validate
echo execute rc.local > /root/data.log

# start tomcat
/root/tomcat-8.5.73/start.sh

重启系统

在重启系统后,可以通过验证 /root/data.log 文件是否存在来确认 rc.local 脚本是否被执行,Tomcat 是否被启动。如果确认 Tomcat 启动成功,则可以通过浏览器访问 http://127.0.0.1:8080 地址来确认 Tomcat 是否启动成功。

说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号