设置关闭宝塔面板依然运行java项目
设置关闭宝塔面板依然运行项目
=================================================
[Unit]
Description=Backend Spring Boot Application
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/www/wwwroot/backend
ExecStart=/usr/bin/java -jar /www/wwwroot/backend/springboot-0.0.1-SNAPSHOT.jar
ExecStop=/bin/kill -15 $MAINPID
Restart=always
RestartSec=10
Environment=SPRING_PROFILES_ACTIVE=prod
# 日志配置
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
=========================================================
# 设置服务文件权限
sudo chmod 644 /etc/systemd/system/backend-app.service
# 重新加载 systemd
sudo systemctl daemon-reload
# 启用开机自启
sudo systemctl enable backend-app
# 启动服务
sudo systemctl start backend-app
# 检查状态(这会显示具体错误)
sudo systemctl status backend-app
=========================================================
# 启动服务
sudo systemctl start backend-app
# 停止服务
sudo systemctl stop backend-app
# 重启服务
sudo systemctl restart backend-app
# 查看状态
sudo systemctl status backend-app
# 查看实时日志
sudo journalctl -u backend-app -f
# 禁用开机自启
sudo systemctl disable backend-app
=========================================================
设置自启动
# 设置所有服务开机启动
sudo systemctl enable nginx
sudo systemctl enable mysqld
sudo systemctl enable backend-app
# 验证
sudo systemctl is-enabled nginx mysqld backend-app
================================================
# 启动所有服务
sudo systemctl start mysqld
sleep 5 # 等待数据库启动
sudo systemctl start backend-app
sleep 3 # 等待后端启动
sudo systemctl start nginx
echo "所有服务启动完成"