4

I am running an executable jar using the command prompt. But after command prompt closing the execution is stopped. I need to run this permanently. Because this is a microservice. How can I achieve this goal I am running using java -jar JARPATH

Saeed Hassanvand
  • 931
  • 1
  • 14
  • 31
K Rajitha
  • 306
  • 7
  • 24

2 Answers2

6

There are several options:

1) Creating a service as was mentioned in the first answer and comment.

2) Run a process in the background.

nohup java -jar my-service.jar &

Where nohup command enables a process to continue running in the background when a user exits a shell.

To terminate the service, you need to kill the process.

You can create a run-script writing PID to a variable (or a text file):

#!/bin/bash
my-service &
export SERVICE_PID=$!

And kill the PID in stop-script:

#!/bin/bash
kill -9 $SERVICE_PID

3) Run process in a container, e.g. Docker

Running process in container allows has some advantages and allows to manage many different options rather than just 'run-and-stop'.

J-Alex
  • 6,881
  • 10
  • 46
  • 64
2

Simply create a service to invoke jar file. Services can run indefinitely and can be triggered at startup or can be started or stopped manually.

Dark Knight
  • 8,218
  • 4
  • 39
  • 58