实现systemctl 管理二进制安装的Tomcat,并开机自启Tomcat
systemctl通过放在/usr/lib/systemd/system/xxx.service 配置文件,实现管理服务的任务
yum安装的自带system,但二进制安装没有,所以需要自己编辑文件
一、这里以系统自带的sshd.service文件来参考
1 [root@zabbix5 system]# systemctl cat sshd 2 # /usr/lib/systemd/system/sshd.service #这个文件的存放位置 3 [Unit] #一般书写注释,说明服务作用,服务的依赖 4 Description=OpenSSH server daemon #注释,描述 5 Documentation=man:sshd(8) man:sshd_config(5) #man帮助 6 After=network.target sshd-keygen.service #这个服务在这些服务后才能运行,表示有依赖 7 Wants=sshd-keygen.service # 依赖 8 9 [Service] #核心部分,指定服务开启,关闭重启分别使用的命令,服务的环境变量配置文件 10 Type=notify #服务的类型 一般有notify oneshot 11 EnvironmentFile=/etc/sysconfig/sshd #指定环境变量的文件 12 ExecStart=/usr/sbin/sshd -D $OPTIONS #服务启动的命令 13 ExecReload=/bin/kill -HUP $MAINPID #服务重启命令 14 KillMode=process #服务关闭方式, 15 Restart=on-failure #重启失败后在次重启 16 RestartSec=42s #重启的间隔是42s 17 18 [Install] #指定运行级别 19 WantedBy=multi-user.target #3模式,多用户模式
二、参考Nginx配置文件编写Tomcat的service
[root@zabbix5 system]# systemctl cat nginx # /usr/lib/systemd/system/nginx.service [Unit] Description=The nginx HTTP and reverse proxy server After=network-online.target remote-fs.target nss-lookup.target Wants=network-online.target [Service] Type=forking PIDFile=/run/nginx.pid # Nginx will fail to start if /run/nginx.pid already exists but # SELinux context. This might happen when running `nginx -t` fro # https://bugzilla.redhat.com/show_bug.cgi?id=1268621 ExecStartPre=/usr/bin/rm -f /run/nginx.pid ExecStartPre=/usr/sbin/nginx -t ExecStart=/usr/sbin/nginx ExecReload=/usr/sbin/nginx -s reload KillSignal=SIGQUIT TimeoutStopSec=5 KillMode=process PrivateTmp=true [Install] WantedBy=multi-user.target
修改如下:
# /usr/lib/systemd/system/tomcat.service [Unit] Description=The Tomcat web server by lvzy v1.0 After=network-online.target remote-fs.target nss-lookup.target Wants=network-online.target [Service] Type=forking EnvironmentFile=/etc/sysconfig/tomcat ExecStart=/app/tools/tomcat/bin/startup.sh ExecStop=/app/tools/tomcat/bin/shutdown.sh ExecReload=/app/tools/tomcat/bin/shutdown.sh && sleep 1 && /app/tools/tomcat/bin/startup.sh [Install] WantedBy=multi-user.target
2、编写环境变量文件
JAVA_HOME=/app/tools/jdk PATH=/app/tools/jdk/bin:/app/tools/jdk/jre/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
##第二行PATH=后的,$JAVA_HOME之前的都是在本地echo $PATH 复制过来的
3、通知系统systemctl配置发生变化,重新加载下配置文件
systemctl daemon-reload
4、systemctl 控制开启,关闭,重启开机启动Tomcat服务
systemctl start tomcat
systemctl stop tomcat
systemctl restart tomcat
systemctl enable tomcat
## 默认8080端口防火墙是没有开放的,需要开放防火墙才能访问
复制下面systemctl.sh脚本,到系统中bash systemctl.sh该脚本即可 (仅仅只需要将下方的/app/tools 替换成你Tomcat所在的目录即可)
[root@test_pc system]# cat /root/systemctl.sh #!/bin/bash #2022-08:17-22:03 #将编写好的Tomcat服务导入到/usr/lib/systemd/system/tomcat.service下 cat >> /usr/lib/systemd/system/tomcat.service << 'EOF' #修改/app/tools为你Tomcat所在的目录 # /usr/lib/systemd/system/tomcat.service [Unit] Description=The Tomcat web server by lvzy v1.0 After=network-online.target remote-fs.target nss-lookup.target Wants=network-online.target [Service] Type=forking EnvironmentFile=/etc/sysconfig/tomcat ExecStart=/app/tools/tomcat/bin/startup.sh ExecStop=/app/tools/tomcat/bin/shutdown.sh ExecReload=/app/tools/tomcat/bin/shutdown.sh && sleep 1 && /app/tools/tomcat/bin/startup.sh [Install] WantedBy=multi-user.target EOF #导入环境变量配置文件 cat >> /etc/sysconfig/tomcat << EOF JAVA_HOME=/app/tools/jdk PATH=/app/tools/jdk/bin:/app/tools/jdk/jre/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar EOF #通知系统重读systemclt配置文件 systemctl daemon-reload
-------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------- 博客均为个人笔记,无所追求,仅供参考~~~ QQ--2382990774