欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用systemd管理java的jar包服务

程序员文章站 2022-05-02 07:55:48
...
一、编写启动和停止脚本
#### 启动脚本
# cat /usr/local/bin/start.sh

#!/bin/bash
export JAVA_HOME="/usr/java/jdk1.8.0_211"
export PATH=$JAVA_HOME/bin:$PATH
ServiceName=$1
CMD_PATH="/opt/services/ds/${ServiceName}"
cd $CMD_PATH/
chmod 755 ${CMD_PATH}/${ServiceName}.jar
nohup java -jar -Xms512m -Xmx1024m ${CMD_PATH}/${ServiceName}.jar &
echo $! > $CMD_PATH/logs/${ServiceName}.pid


#### 停止脚本
# cat /usr/local/bin/stop.sh

#!/bin/bash
ServiceName=$1
CMD_PATH="/opt/services/ds/${ServiceName}"
PID=$(cat $CMD_PATH/logs/${ServiceName}.pid)
kill -9 $PID
rm -f nohup.out
二、将服务使用systemd管理服务
# cat /usr/lib/systemd/system/smsd.service

# Systemd unit file for tomcat
[Unit]
Description=sms service daemon
After=syslog.target network.target

[Service]
Type=forking

Environment=JAVA_HOME="/usr/java/jdk1.8.0_211"
ExecStart=/usr/local/bin/start.sh "sms-service"
ExecStop=/usr/local/bin/stop.sh "sms-service"
#ExecReload=/usr/local/bin/restart.sh "sms-service"
PrivateTmp=true

User=app
Group=app

[Install]
WantedBy=multi-user.target
三、测试
systemctl daemon-reload
systemctl start smsd
systemctl stop smsd
systemctl enable smsd

使用systemd管理java的jar包服务

相关标签: systemd管理jar