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

3

7

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?

Corie answered 27/7, 2020 at 19:46 Comment(2)
write a bash script . Include in bash CLI , the list of args above. ie the bash is a wrapper for java cli args. Then just call that wrapper shell from the apps property in the jsonDole
@RobertRowntree Thanks!! Just the bash script was enough to solve my problem. After creating the script and giving it execution permission I can start my service using pm2 start graphhopper.sh --name=graphhopper . The file of my question turns out not being necessary.Corie
C
14

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
Corie answered 29/7, 2020 at 1:11 Comment(0)
P
3

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
Pretermit answered 4/12, 2020 at 0:15 Comment(1)
where is the config.ymlMarcus
D
0

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
Donothing answered 4/8, 2022 at 3:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.