准备启动jar包,app.jar放入指定目录。
java -jar app.jar
nohup java -jar app.jar >/dev/null 2>&1 &
加入配置参数命令
nohup java -Xms512M -Xmx512M -jar app.jar --server.port=9080 spring.profiles.active=prod >nohup.out 2>&1 &
随着配置参数越来越多,可创建启动脚本一键启动。
vi start.sh
拷贝一下内容
#!/bin/bash
#自定义
RESOURCE_NAME=test-server
SERVER_PORT=8080
#停止服务
PID=$(ps -ef | grep $RESOURCE_NAME | grep -v grep | awk '{ print $2 }')
if [ -z "$PID" ]
then
echo $RESOURCE_NAME Application is already stopped
else
kill $PID
echo $RESOURCE_NAME Application stopped successfully pid = $PID
fi
#不输出启动日志
nohup java -jar $RESOURCE_NAME.jar \
--server.port=$SERVER_PORT \
--spring.profiles.active=prod \
>/dev/null 2>&1 &
#不输出日志
#>/dev/null 2>&1 &
#输出错误日志
#>/dev/null 2>nohup_err.out &
#输出全部日志
#>nohup.out 2>&1 &
echo $RESOURCE_NAME Application started successfully
脚本授权
chmod 775 start.sh
vi app.sh
拷贝一下内容
#!/bin/sh
# ./app.sh start 启动 stop 停止 restart 重启 status 状态
AppName=app-server.jar
#应用端口
SERVER_PORT=8080
# JVM参数
JVM_OPTS="-Dname=$AppName -Duser.timezone=Asia/Shanghai -Xms512m -Xmx512m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:NewRatio=1 -XX:SurvivorRatio=30 -XX:+UseParallelGC -XX:+UseParallelOldGC"
if [ "$1" = "" ];
then
echo -e "\033[0;31m 未输入操作名 \033[0m \033[0;34m {start|stop|restart|status} \033[0m"
exit 1
fi
if [ "$AppName" = "" ];
then
echo -e "\033[0;31m 未输入应用名 \033[0m"
exit 1
fi
function start()
{
PID=`ps -ef |grep java|grep $AppName|grep -v grep|awk '{print $2}'`
if [ x"$PID" != x"" ]; then
echo "$AppName is running..."
else
nohup java $JVM_OPTS -jar $AppName\
--server.port=$SERVER_PORT \
--spring.profiles.active=mysql-test \
--jasypt.encryptor.password=123456 \
> /dev/null 2>&1 &
echo "Start $AppName success..."
fi
}
function stop()
{
echo "Stop $AppName"
PID=""
query(){
PID=`ps -ef |grep java|grep $AppName|grep -v grep|awk '{print $2}'`
}
query
if [ x"$PID" != x"" ]; then
kill -TERM $PID
echo "$AppName (pid:$PID) exiting..."
while [ x"$PID" != x"" ]
do
sleep 1
query
done
echo "$AppName exited."
else
echo "$AppName already stopped."
fi
}
function restart()
{
stop
sleep 2
start
}
function status()
{
PID=`ps -ef |grep java|grep $AppName|grep -v grep|wc -l`
if [ $PID != 0 ];then
echo "$AppName is running..."
else
echo "$AppName is not running..."
fi
}
case $1 in
start)
start;;
stop)
stop;;
restart)
restart;;
status)
status;;
*)
esac
1、启动脚本拷贝至/etc/rc.d/init.d目录
注:需要把启动脚本中的路径都修改成绝对路径
2.给脚本app-server-auto.sh和jar包app-server.jar 添加权限
sudo chmod +x /etc/init.d/app-server-auto.sh
sudo chmod +x /home/xxx/app-server.jar
3.添加到chkconfig 并把chkconfig作为系统服务 然后设置开机自启
#添加为系统服务
sudo chkconfig --add app-server-auto.sh
#设置为开机启动
sudo chkconfig app-server-auto.sh on
# 启动服务 查看脚本是否正确
sudo service app-server-auto.sh start
1、编辑/etc/rc.d/rc.local文件
vi /etc/rc.d/rc.local
2、拷贝以下内容
source /etc/profile && cd /home/hello/application/app-server && ./start.sh
重启机器就可以测试了
#创建目录
mkdir /var/app
#创建组
getent group app|| groupadd -r app
#创建用户
getent passwd app || useradd -r -d /opt -s /bin/nologin -g app app
chown -R app:app /var/app
在/usr/lib/systemd/system目录下增加app.service,目录必须是绝对目录
vi /usr/lib/systemd/system/app.service
拷贝以下内容
[Unit]
Description=App-server description
After=syslog.target
[Service]
User=app
Group=app
ExecStart=/usr/java/jdk1.8.0_202/bin/java -Xms512M -Xmx512M -jar /var/app/app-server.jar
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
刷新命令
systemctl daemon-reload
#配置开机启动
systemctl enable app
#删除开机启动
systemctl disable app
#启动
systemctl start app
#停止
systemctl stop app
#重启
systemctl restart app
查看启动状态
systemctl status app
#刷新服务
systemctl daemon-reload
#查看日志
journalctl -u springboot.service
推荐一个免费的pdf转换网站,包含pdf转word、pdf拆分等功能。