问题描述
我正在尝试从Linux中的systemd服务启动jar.我正在执行的jar假设当前目录中有一个xml文件.我正在使用-config参数传递该文件的名称.我创建的示例服务如下:
I am attempting to start a jar from a systemd service, in linux. The jar that I am executing assumes that within the current directory, there is an xml file. I am passing the name of that file, with the -config argument. The sample service I have created is below:
[单位]说明=我的服务After = network.target
[服务]类型=简单Environment ="xml_file_name = sample.xml"ExecStart =/usr/bin/java -jar/path/to/jar/myapp.jar -config $ {xml_file_name}
上面的服务文件位于/usr/lib/systemd/system目录中,称为myservice.service.我正在执行以下命令来启动它:
The service file above is placed in the /usr/lib/systemd/system directory, and is called myservice.service. I am doing the following commands to start it:
systemctl守护程序重新加载systemctl stop myservice.servicesystemctl启动myservice.servicesystemctl状态myservice.service
systemctl status myservice.service
命令显示jar文件已运行,但是我的应用程序说找不到$ {xml_file_name}.
The systemctl status myservice.service
command shows that the jar file ran, but my application says that it cannot find ${xml_file_name}.
此外,我的jar声明它是从/目录执行的.我相信这是问题的一部分,因为$ {xml_file_name}仅适用于/path/to/jar/目录.
In addition, my jar states that it was executed from the / directory. I believe that this is part of the problem, because the ${xml_file_name} is only applicable in the /path/to/jar/ directory.
尝试过的事情
-
-Xbootclasspath/p:"/path/to/jar/"
:在jar的路径前添加位置,以便也许可以看到$ {xml_file_name}.
-Xbootclasspath/p:"/path/to/jar/"
: prepend the path of the jarslocation, so that maybe the ${xml_file_name} can be seen.
更改了/path/to/jar/以确保已启用所有可能的权限
changed the /path/to/jar/ to make sure it has all possible permissions enabled
我试图在系统服务的 [Service]
部分下添加 User = root
,但没有任何改变.不管哪种方式,只有root用户在计算机上,并且所有权限似乎都已签出.
I tried to add User=root
under the [Service]
section of my systemd service, but it made no change. Either way, only root user is on the machine, and the permissions all seem to check out.
更使我感到奇怪的是,如果我cd到/,然后手动执行:
What makes this even more strange is that if I cd to / , and then manually execute:
/usr/bin/java -jar/path/to/jar/myapp.jar -config sample.xml
所有单词都很好.
这里是否有明显的我想念的东西?是否可以告诉systemd服务,执行此Java jar,但请确保工作路径是/path/to/jar/
而不是/
?
Is there something evident that I am missing here? Is it possible to tell systemd service, execute this java jar, but make sure that the working path is /path/to/jar/
as opposed to /
?
推荐答案
为解决此问题,我最终在 [Service]
部分下使用了以下属性:
To solve this, I ended up using the following attribute under the [Service]
section:
[服务]...WorkingDirectory =/路径/到/jarExecStart =/usr/bin/java -jar my.jar -config sample.xml
解决了问题!
这篇关于在systemd中执行jar文件时强制工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!