How can I start a java application with parameters using a pm2 config file?

牧云@^-^@ 提交于 2021-02-11 13:45:03

问题


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

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"
     }
   ]
}

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?


回答1:


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

And to start it as a service with pm2:

pm2 start graphhopper.sh --name=graphhopper



回答2:


You can also run a fat jar directly in pm2 - use two dashes to separate the command args:

pm2 start java --  -jar matching-web/target/graphhopper-map-matching-web-1.0-SNAPSHOT.jar server config.yml


来源:https://stackoverflow.com/questions/63122472/how-can-i-start-a-java-application-with-parameters-using-a-pm2-config-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!