创建服务配置文件
创建.service文件
以创建tomcat服务为例。通常tomcat的启动、停止、重启命令如下
/usr/local/tomcat/bin/startup.sh 启动tomcat的脚本
/usr/local/tomcat/bin/shutdown.sh 关闭tomcat的脚本
/usr/local/tomcat/bin/restart.sh 重启tomcat的脚本
我们可以配置成一个服务,在/usr/lib/systemd/system/下创建tomcat.service
[Unit]
Description=java tomcat
After=tomcat service
[Service]
Type=forking
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecReload=/usr/local/tomcat/bin/restart.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
.service文件详解
[Unit]服务说明
Description # 服务描述
After # 依赖服务,依赖启动后,该服务才会启动
[Service] # 服务参数设置
Type=forking # 服务以后台启动
ExecStart=/usr/local/tomcat/bin/startup.sh # 开始命令
ExecReload=/usr/local/tomcat/bin/restart.sh # 重启命令
ExecStop=/usr/local/tomcat/bin/shutdown.sh # 关闭命令
PrivateTmp # 是否给服务分配临时独立空间
WantedBy=multi-user.target # 多用户
开启服务
重载系统服务
systemctl daemon-reload
开机自启动
systemctl enable tomcat
systemctl enable tomcat.service
关闭开机自启动
systemctl disable tomcat
systemctl disable tomcat.service
开启服务
systemctl start tomcat
systemctl start tomcat.service
关闭服务
systemctl stop tomcat
systemctl stop tomcat.service
重启服务
systemctl restart tomcat
systemctl restart tomcat.service
查看服务状态
systemctl status tomcat
systemctl status tomcat.service
评论区