一、编写启动和停止脚本
#### 启动脚本
# 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
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8b872bde87380077cbd9437ca6e9bdd2.png)