如何使用 pm2 配置文件启动带有参数的 Java 应用程序? [英] How can I start a java application with parameters using a pm2 config file?
问题描述
我正在尝试使用 pm2
启动 graphhopper...graphhopper
是一个 Java 应用程序,我在终端上启动它的方式是转到其文件夹并输入以下命令:
I'm trying to start graphhopper using pm2
... graphhopper
is a java application and the way I initiate it on the terminal is by going to its folder and entering the following command:
java -jar matching-web/target/graphhopper-map-matching-web-1.0-SNAPSHOT.jar server config.yml
这个应用程序从命令行运行得很好,但我没有成功地将它作为使用 pm2
的服务运行.我正在使用的配置文件是这个(pm2 start config.json
):
This application works fine running from the command line, but I haven't succeeded on running it as a service with pm2
. The config file I'm using is this one (pm2 start config.json
):
{
"apps":[
{
"name":"graphhopper",
"cwd":".",
"script":"/usr/bin/java",
"args":[
"-jar",
"/home/myyser/graphhopper/map-matching/matching-web/target/graphhopper-map-matching-web-1.0-SNAPSHOT.jar",
"server",
"config.yml"
],
"log_date_format":"YYYY-MM-DD HH:mm Z",
"exec_interpreter":"",
"exec_mode":"fork"
}
]
}
我 100% 确定我在这里出错的是我编写 server"、config.yml"
参数的方式......查看 pm2 logs graphhopper
我可以看到这些参数根本没有被识别......我也试图调整它的完成方式,但我没有设法找出正确的解决方案.我知道如何使用不带参数的 pm2
启动 Java 应用程序.但是,我如何使用具有 graphhopper
的参数的 Java 应用程序来做到这一点?
I'm 100% sure that what I'm getting wrong here is the way I'm writing the "server", "config.yml"
parameters... Looking into pm2 logs graphhopper
I can see that those parameters are not being recognized at all... I've tried to tweak the way it's done as well but I didn't manage to figure out the right solution. I know how to start a java application using pm2
with no parameters. But how can I do it with a java application that has parameters as in the case of graphhopper
?
推荐答案
如评论中所述,这个问题可以通过创建一个 bash 脚本并使用 pm2
运行它来解决,而不是直接运行java应用程序...使用的bash脚本是文件graphhopper.sh
,如下所示:
As stated in the comments, this issue can be solved by creating a bash script and running it with pm2
instead of running directly the java application... The bash script used was the file graphhopper.sh
as the following:
#!/bin/bash
java -jar matching-web/target/graphhopper-map-matching-web-1.0-SNAPSHOT.jar server config.yml
并使用 pm2
将其作为服务启动:
And to start it as a service with pm2
:
pm2 start graphhopper.sh --name=graphhopper
这篇关于如何使用 pm2 配置文件启动带有参数的 Java 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!