最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

environment variables - url property wrongly treated as relative address when spring boot project run as a systemd service - Sta

programmeradmin2浏览0评论

I have a Spring Boot project that runs fine on my remote Ubuntu server (22.04) when run from the command line (via ssh). In this project, I am providing an H2 database location in the application.properties file with the help of an environment variable. The line that provides the URL is the following:

spring.datasource.url=jdbc:h2:${PATH_TO_MYPROJECT_FOLDER}MyProjectDB

The error message (when starting via the command sudo systemctl start myproject) is the following:

A file path that is implicitly relative to the current working directory is 
not allowed in the database URL "jdbc:h2:${PATH_TO_MYPROJECT_FOLDER}MyProjectDB". 
Use an absolute path, ~/name, ./name, or the baseDir setting instead.

The value of PATH_TO_MYPROJECT_FOLDER is "/var/lib/myprojectfolder/" which is an absolute address, yes?

Any thoughts as to why the project would run one way and not the other?

Here is the systemd configuration file. This file, or the bash file it references that has the java -jar ... command in it, are maybe part of the problem?

myproject.service:

#
# Systemd configuration file for MyProject
#

[Unit]
Description=Service MyProject
After=syslog.target

[Service]

# Lifecycle
Type=simple
ExecStart=/var/lib/myprojectfolder/runMyProject
SuccessExitStatus=143

# Security
User=myprojectuser

[Install]
WantedBy=multi-user.target

The executable file being referenced (runMyProject) is the following:

#!/bin/bash
/usr/lib/jvm/java-17-openjdk-amd64/bin/java -jar /var/lib/myprojectfolder/myproject.jar

I have a Spring Boot project that runs fine on my remote Ubuntu server (22.04) when run from the command line (via ssh). In this project, I am providing an H2 database location in the application.properties file with the help of an environment variable. The line that provides the URL is the following:

spring.datasource.url=jdbc:h2:${PATH_TO_MYPROJECT_FOLDER}MyProjectDB

The error message (when starting via the command sudo systemctl start myproject) is the following:

A file path that is implicitly relative to the current working directory is 
not allowed in the database URL "jdbc:h2:${PATH_TO_MYPROJECT_FOLDER}MyProjectDB". 
Use an absolute path, ~/name, ./name, or the baseDir setting instead.

The value of PATH_TO_MYPROJECT_FOLDER is "/var/lib/myprojectfolder/" which is an absolute address, yes?

Any thoughts as to why the project would run one way and not the other?

Here is the systemd configuration file. This file, or the bash file it references that has the java -jar ... command in it, are maybe part of the problem?

myproject.service:

#
# Systemd configuration file for MyProject
#

[Unit]
Description=Service MyProject
After=syslog.target

[Service]

# Lifecycle
Type=simple
ExecStart=/var/lib/myprojectfolder/runMyProject
SuccessExitStatus=143

# Security
User=myprojectuser

[Install]
WantedBy=multi-user.target

The executable file being referenced (runMyProject) is the following:

#!/bin/bash
/usr/lib/jvm/java-17-openjdk-amd64/bin/java -jar /var/lib/myprojectfolder/myproject.jar
Share Improve this question edited Jan 19 at 3:59 Phil Freihofner asked Jan 19 at 3:25 Phil FreihofnerPhil Freihofner 7,9101 gold badge23 silver badges45 bronze badges 4
  • 1 Speaking from a point of ignorance, it's telling that the URL in the error message has not expanded the variable. Are those supposed to be expanded? Is the syntax correct? – Tim Roberts Commented Jan 19 at 4:02
  • Good observation! I was having trouble finding documentation on the expected behavior, but just found the following page and have a couple ideas to try (not that I can see my specific case being mentioned). docs.spring.io/spring-boot/reference/features/… Will report back when I get a chance. – Phil Freihofner Commented Jan 20 at 4:32
  • 'The value of PATH_TO_MYPROJECT_FOLDER is "/var/lib/myprojectfolder/"' – where is that value actually defined? I don't see it anywhere in your systemd unit nor in your (entirely redundant) run script. – grawity_u1686 Commented Jan 20 at 8:11
  • Thank you @grawity_u1686 for pointing out the redundancy. I have deleted the bash script with "java -jar " command in it and put that line in the ExecStart. I previously tried just having the jar file in the ExecStart and of course that didn't work! Rookie mistakes. As for the definition of the env var, I had defined it in /etc/environment and was under the impression that this would be picked up by any user, as this is the location for system level environment variables. For whatever reason, I did not realize from your comment, that entry in the .service was a possibility. – Phil Freihofner Commented Jan 25 at 5:05
Add a comment  | 

1 Answer 1

Reset to default 0

I was able to achieve my goal with the following change to the project. In the myproject.service file, I added a [Service] line:

Environment=PATH_TO_MYPROJECTDB=/var/lib/myprojectfolder/MyProjectDB

When running from the command line via an SSH shell, the system environment variables (as defined in /etc/environment) are visible/expandable when invoked in the application.properties file. But, apparently these variables are not expanded when running the service using systemd. This takes me by surprise. I thought system variables were visible to all users.

I'd mention that the variable assignment done this way is somewhat visible (for example, they can be viewed via the command systemctl show myproject). If the property value needs to be kept secret, it's better to use EnvironmentFile and have a separate file with the secret information. This is explained pretty well here Using environment variables in systemd units

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论