Ubuntu中通过SSM文档运行Systemd服务时无法访问Jar文件
Spring PetClinic部署故障排查与修复
问题背景
通过AWS SSM文档将Spring PetClinic部署至Ubuntu 22服务器,服务启动后反复失败,执行sudo journalctl -u spring-petclinic.service返回找不到jar文件的错误,尝试权限配置、以root用户运行均未解决。
错误日志
May 17 21:52:36 ip-10-0-4-141 systemd[1]: spring-petclinic.service: Failed with result 'exit-code'. May 17 21:52:36 ip-10-0-4-141 systemd[1]: spring-petclinic.service: Scheduled restart job, restart counter is at 3. May 17 21:52:36 ip-10-0-4-141 systemd[1]: Stopped Spring PetClinic Application. May 17 21:52:36 ip-10-0-4-141 systemd[1]: Started Spring PetClinic Application. May 17 21:52:36 ip-10-0-4-141 java[7076]: Error: Unable to access jarfile /clinic/spring-petclinic/target/*.jar May 17 21:52:36 ip-10-0-4-141 systemd[1]: spring-petclinic.service: Main process exited, code=exited, status=1/FAILURE May 17 21:52:36 ip-10-0-4-141 systemd[1]: spring-petclinic.service: Failed with result 'exit-code'. May 17 21:52:36 ip-10-0-4-141 systemd[1]: spring-petclinic.service: Scheduled restart job, restart counter is at 4. May 17 21:52:36 ip-10-0-4-141 systemd[1]: Stopped Spring PetClinic Application. May 17 21:52:36 ip-10-0-4-141 systemd[1]: Started Spring PetClinic Application. May 17 21:52:36 ip-10-0-4-141 java[7077]: Error: Unable to access jarfile /clinic/spring-petclinic/target/*.jar May 17 21:52:36 ip-10-0-4-141 systemd[1]: spring-petclinic.service: Main process exited, code=exited, status=1/FA
故障根源分析
查看提供的SSM运行文档,核心问题出在CreateSystemdService步骤:
- systemd服务文件格式错误:生成
spring-petclinic.service时,[Unit]区块内混入了echo命令,导致systemd无法正确解析服务配置。 - 通配符未被解析:
ExecStart中的*.jar通配符不会被systemd自动解析,必须通过shell处理。 - 冗余操作:重复清空并重置
/etc/environment属于不必要操作,可能导致环境变量异常。
修复步骤
1. 修正SSM文档的CreateSystemdService步骤
替换原脚本内容,确保生成合法的systemd服务文件,并通过shell解析通配符:
--- schemaVersion: '2.2' description: Install Java 17 and run the Spring PetClinic application parameters: {} mainSteps: # 保留InstallJava17和DownloadApplication步骤不变,仅修改CreateSystemdService - name: CreateSystemdService action: aws:runShellScript inputs: runCommand: - | #!/bin/bash # 确保JAVA_HOME环境变量正确 JAVA_HOME=$(sudo update-alternatives --config java | grep -o -E '/usr/lib/jvm/[a-zA-Z0-9-]+') echo "JAVA_HOME=\"$JAVA_HOME\"" | sudo tee -a /etc/environment # 生成合法的systemd服务文件 sudo tee /etc/systemd/system/spring-petclinic.service <<EOF [Unit] Description=Spring PetClinic Application After=network.target [Service] # 使用shell解析通配符,确保找到正确的jar文件 ExecStart=/bin/sh -c "$JAVA_HOME/bin/java -jar /clinic/spring-petclinic/target/*.jar" Restart=always User=root # 设置工作目录,避免路径问题 WorkingDirectory=/clinic/spring-petclinic [Install] WantedBy=multi-user.target EOF # 重新加载systemd配置并启动服务 sudo systemctl daemon-reload sudo systemctl enable spring-petclinic sudo systemctl start spring-petclinic
2. 验证jar文件生成(可选)
在DownloadApplication步骤中添加构建校验,确保Maven构建成功生成jar:
# 在./mvnw package后添加以下内容 if [ ! -f target/*.jar ]; then echo "Maven构建失败,未生成jar文件" exit 1 fi
验证修复效果
- 检查生成的服务文件是否合法:
cat /etc/systemd/system/spring-petclinic.service
确保文件中无多余echo命令,ExecStart格式正确。
- 启动服务并查看状态:
sudo systemctl start spring-petclinic sudo systemctl status spring-petclinic
- 实时查看服务日志:
sudo journalctl -u spring-petclinic.service -f
内容的提问来源于stack exchange,提问作者SovietFrontier
相关产品推荐
相关产品推荐

