使用Systemctl启动项目
一、前提条件
- 操作系统:Linux(内核 ≥ 3.0,支持 systemd)。
- 拥有
root
或具有相应权限的用户。 - 已联网,能访问外部下载源。
二、安装 Java 1.8.0_411
创建下载目录并下载 JDK:
mkdir -p /opt/package/java && cd /opt/package/java wget https://github.com/litongjava/oracle-jdk/releases/download/8u411/jdk-8u411-linux-x64.tar.gz
解压到系统标准路径:
mkdir -p /usr/java tar -xf jdk-8u411-linux-x64.tar.gz -C /usr/java
配置环境变量(可添加到
/etc/profile
或~/.bashrc
):export JAVA_HOME=/usr/java/jdk1.8.0_411 export PATH=$JAVA_HOME/bin:$PATH
验证安装:
java -version
应输出:
java version "1.8.0_411" Java(TM) SE Runtime Environment (build 1.8.0_411-b09) Java HotSpot(TM) 64-Bit Server VM (build 25.411-b09, mixed mode)
三、安装 Maven 3.8.8
创建下载目录并获取压缩包:
mkdir -p /opt/package/maven && cd /opt/package/maven wget https://dlcdn.apache.org/maven/maven-3/3.8.8/binaries/apache-maven-3.8.8-bin.zip
解压并配置:
mkdir -p /usr/maven unzip apache-maven-3.8.8-bin.zip -d /usr/maven
设置环境变量(同样可写入
/etc/profile
或~/.bashrc
):export MVN_HOME=/usr/maven/apache-maven-3.8.8 export PATH=$MVN_HOME/bin:$PATH
验证安装:
mvn --version
应输出类似:
Apache Maven 3.8.8 (...) Java version: 1.8.0_411, vendor: Oracle Corporation
四、下载并编译项目
创建应用存放目录并下载源码:
mkdir -p /data/apps && cd /data/apps wget https://gitee.com/ppnt/tio-boot-web-hello/repository/archive/main.zip yum install -y unzip unzip main.zip mv tio-boot-web-hello-main tio-boot-web-hello
进入项目目录并执行打包:
cd /data/apps/tio-boot-web-hello mvn clean package -Pproduction -DskipTests
本地测试启动(可选):
java -jar target/tio-boot-web-hello-1.0.0.jar
五、配置 systemd 用户级服务
说明:使用
systemctl --user
能让非 root 用户在自身会话中管理服务。这里以root
用户为例,若使用其他用户,请确保目录权限和用户一致。
创建用户级服务目录:
mkdir -p ~/.config/systemd/user
新建服务文件
~/.config/systemd/user/tio-boot-web-hello.service
,内容如下:[Unit] Description=tio-boot-web-hello Java Web Service After=network.target [Service] Type=simple User=root WorkingDirectory=/data/apps/tio-boot-web-hello ExecStart=/usr/java/jdk1.8.0_411/bin/java -jar /data/apps/tio-boot-web-hello/target/tio-boot-web-hello-1.0.0.jar Restart=on-failure RestartSec=5s [Install] WantedBy=default.target
- WorkingDirectory:服务启动前切换到的目录
- ExecStart:完整的 Java 可执行路径与 JAR 包路径
- WantedBy=default.target:对应
systemctl --user enable
时的目标
重新加载用户级服务配置:
systemctl --user daemon-reload
六、启动并管理服务
启动服务:
systemctl --user start tio-boot-web-hello.service
查看运行状态:
systemctl --user status tio-boot-web-hello.service
正常输出应类似:
● tio-boot-web-hello.service – tio-boot-web-hello Java Web Service Loaded: loaded (/home/root/.config/systemd/user/tio-boot-web-hello.service; enabled) Active: active (running) since ... CGroup: /user.slice/user-0.slice/user@0.service/.../tio-boot-web-hello.service └─xxxx /usr/java/jdk1.8.0_411/bin/java ...
设置开机自动启动:
systemctl --user enable tio-boot-web-hello.service
列出所有用户级服务及其状态:
systemctl --user list-units --type=service
七、常见问题及优化建议
日志输出 默认日志会打印到控制台;若需重定向到文件,可在服务文件里添加:
StandardOutput=append:/var/log/tio-boot-web-hello.out StandardError=append:/var/log/tio-boot-web-hello.err
环境变量 若依赖额外环境变量,可在
[Service]
段添加:Environment="JAVA_HOME=/usr/java/jdk1.8.0_411" "PATH=/usr/java/jdk1.8.0_411/bin:/usr/maven/apache-maven-3.8.8/bin:$PATH"
安全性 — 尽量避免以
root
运行服务,可创建专用用户并调整文件权限。 — 将 JAR 包及相关配置放置在只读目录或使用 SELinux 进行约束。
至此,您已完成从环境准备、源码编译到 systemd 用户级服务配置与管理的全流程。