一、准备工作
将jar包上传到服务器(本文将jar包上传到/home/project/jar/)
新建脚本文件(本文将脚本文件放在/home/project/sh/文件下)
注: sentinel-dashboard是我的程序名,可根据实际情况替换
二、新建、编辑jar包的启动和停止脚本
# 启动脚本文件
vim /home/project/sh/sentinel-dashboard-start.sh
# 停止脚本文件
vim /home/project/sh/sentinel-dashboard-stop.sh
编辑 sentinel-dashboard-start.sh
#!/bin/sh
# 启动项目(如果不是root用户 需要在前面加sudo,在后面加密码)
#其中“ > /home/project/jar/nohup.out 2>&1 &”是nohup输出日志
nohup java -jar /home/project/jar/sentinel-dashboard.jar > /home/project/jar/nohup.out 2>&1 &
# 导出项目启动的pid(用于关闭)
echo $! > /var/run/sentinel-dashboard.pid
编辑 sentinel-dashboard-stop.sh
#!/bin/sh
# 获取启动的pid
PID=$(cat /var/run/sentinel-dashboard.pid)
# 关闭
kill -9 $PID
测试执行是否报错
sh /home/project/sh/sentinel-dashboard-start.sh
sh /home/project/sh/sentinel-dashboard-stop.sh
三、对文件设置权限
chmod -R 777 /home/project/jar/sentinel-dashboard.jar
chmod -R 777 /home/project/sh/sentinel-dashboard-start.sh
chmod -R 777 /home/project/sh/sentinel-dashboard-stop.sh
四、配置system文件
在/etc/systemd/system/下新建文件jar.service,名称可自己定义
# 编辑service文件
vim /etc/systemd/system/jar.service
[Unit]
# 修改为自己的服务名称
Description=sentinel-dashboard
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
# 修改为自己的start.sh脚本的绝对路径
ExecStart=/home/project/sh/sentinel-dashboard-start.sh
# 修改为自己的stop.sh脚本的绝对路径
ExecStop=/home/project/sh/sentinel-dashboard-stop.sh
Type=forking
PrivateTmp=true
[Install]
WantedBy=multi-user.target
五、设置开机自启、启动服务
cd /etc/systemd/system/
# 将jar添加到开机自启服务中
systemctl enable jar.service
# 系统服务刷新
systemctl daemon-reload
# 启动服务
systemctl start jar.service
# 查看服务状态
systemctl status jar
# 停止服务
systemctl stop jar
# 取消开机自启
systemctl disable jar
六、 防火墙相关命令
# 查看防火墙状态
systemctl status firewalld
# 启动防火墙
systemctl start firewalld
# 关闭哦防火墙
systemctl stop firewalld
# 禁止开机启动
sudo systemctl disable firewalld
# 添加允许特定端口通过的规则 (例如开放TCP协议的8080端口)
firewall-cmd --zone=public --add-port=8080/tcp --permanent
# 重新加载firewalld配置
firewall-cmd --reload
# 显示当前开放(激活)的端口
firewall-cmd --list-ports
标签:jar,sh,project,自启,dashboard,Linux,sentinel,home
From: https://blog.csdn.net/saonian0929/article/details/140825059