linux-tomcat以服务管理

linux-tomcat以服务管理

centos7 jkd8 tomcat9为例

1、安装jdk tomcat(步骤略)

tomcat安装路径
/usr/local/apache-tomcat-9.0.40

2、配置service

进入路径/usr/lib/systemd/system,创建一个以.service结尾的文件,例如tomcat9.service
vim tomcat9.service

[Unit]
Description=tomcat9
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/apache-tomcat-9.0.40/bin/startup.sh
ExecStop=/usr/local/apache-tomcat-9.0.40/bin//shutdown.sh
ExecReload=/bin/kill -s HUP $MAINPID
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

在上述配置文件中,我们需要注意的是配置好ExecStart和ExecStop
ExecStart对应的是tomcat的启动脚本
ExecStop对应的是tomcat的停止脚本

使用systemctl控制服务

#查看服务状态
systemctl status tomcat9

# 服务未启动时
tomcat9.service - tomcat9
   Loaded: loaded (/usr/lib/systemd/system/tomcat9.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

# 服务启动后
 tomcat9.service - tomcat9
   Loaded: loaded (/usr/lib/systemd/system/tomcat9.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2020-12-05 14:02:46 CST; 57s ago
  Process: 24118 ExecStart=/usr/local/apache-tomcat-9.0.40/bin/startup.sh (code=exited, status=0/SUCCESS)
    Tasks: 35
   Memory: 101.1M
   CGroup: /system.slice/tomcat9.service
           ├─24131 /bin/sh /usr/local/apache-tomcat-9.0.40/bin/catalina.sh start
           ├─24132 /usr/local/sbin/cronolog /usr/local/apache-tomcat-9.0.40/logs/catalina.out.%Y-%m-%d
           └─24133 /usr/bin/java -Djava.util.logging.config.file=/usr/local/apache-tomcat-9.0.40/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogMana...

# 服务停止后
● tomcat9.service - tomcat9
   Loaded: loaded (/usr/lib/systemd/system/tomcat9.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Sat 2020-12-05 14:06:56 CST; 3s ago
  Process: 26135 ExecStop=/usr/local/apache-tomcat-9.0.40/bin//shutdown.sh (code=exited, status=0/SUCCESS)

Dec 05 14:02:46 centos7 systemd[1]: Starting tomcat9...
Dec 05 14:02:46 centos7 startup.sh[24118]: Tomcat started.
Dec 05 14:02:46 centos7 systemd[1]: Started tomcat9.
Dec 05 14:06:56 centos7 systemd[1]: Stopping tomcat9...
Dec 05 14:06:56 centos7 systemd[1]: Stopped tomcat9.
           

#启动tomcat服务
systemctl start tomcat9

#停止tomcat服务
systemctl stop tomcat9

#设置tomcat开机自启动
systemctl enable tomcat9

Created symlink from /etc/systemd/system/multi-user.target.wants/tomcat9.service to /usr/lib/systemd/system/tomcat9.service.

#关闭tomcat开机自启动
systemctl disable tomcat9

Removed symlink /etc/systemd/system/multi-user.target.wants/tomcat9.service.

小尾巴~~
只要有积累,就会有进步

你可能感兴趣的:(linux,centos,tomcat,linux,systemd)