Search code examples
node.jspm2

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


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?


Solution

  • 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