Rafael Muynarsk
Rafael Muynarsk

Reputation: 694

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?

Upvotes: 7

Views: 19282

Answers (3)

johndpope
johndpope

Reputation: 5249

Mine java app worked fine - just use 1 arg - no array.

apps:
  - name : 'admin'
    script: '/opt/homebrew/opt/openjdk@11/bin/java'
    args:  '-jar ./wweevvAdmin/target/wweevvAdmin-1.0-SNAPSHOT.jar'
    instances: '1'
    autorestart: true

Upvotes: 0

LeslieM
LeslieM

Reputation: 2303

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

Upvotes: 4

Rafael Muynarsk
Rafael Muynarsk

Reputation: 694

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

Upvotes: 14

Related Questions