JDK 8 内存参数:

-XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=128m

先创建基本的start、stop、restart脚本:

jarStart.sh脚本:

#!/bin/sh
nohup java -jar /root/myjar-1.0.1-SNAPSHOT.jar > myjar.log 2>&1 &
echo Start Success!

jarStop.sh脚本:

#!/bin/sh
PID=`ps -ef | grep java | grep -v grep | awk '{print $2}'`
if [ "" != "$PID" ]; then
 echo "killing $PID"
 kill -9 $PID
 echo "Java stoped."
else
 echo "Java not running!"
fi

jarRestart.sh脚本:

#!/bin/sh
PID=`ps -ef | grep java | grep -v grep | awk '{print $2}'`
if [ "" != "$PID" ]; then
 echo "killing $PID"
 kill -9 $PID
 echo "Java stoped."
else
 echo "Java not running!"
fi
nohup java -jar /root/myjar-1.0.1-SNAPSHOT.jar > myjar.log 2>&1 &
echo $! > tpid
echo Start Success!

赋予以上sh脚本权限:

chmod +x *.sh

新建开机自启动service脚本,以myjar.service命名:

cd /usr/lib/systemd/system
vi myjar.service

输入以下内容;(start、stop、restart脚本路径要根据自己情况更改)

[Unit]
Description=myjar
After=syslog.target network.target

[Service]
Type=forking
ExecStart=/root/jarStart.sh
ExecReload=/root/jarRestart.sh
ExecStop=/root/jarStop.sh
PrivateTmp=true
User=root
Group=root

[Install]
WantedBy=multi-user.target

赋予777权限:

chmod 777 myjar.service

设置service脚本开机自启动:

systemctl enable myjar.service

其他命令:

# 启动
systemctl start myjar
# 查看运行状态
systemctl status myjar
# 停止
systemctl stop myjar

# 加入开机自启
systemctl enable myjar
# 取消开机自启
systemctl disable myjar
# 判断服务是否处于开机自启状态,输出enabled即代表开机自启
systemctl is-enabled myjar