Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executable jar run permanently [duplicate]

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

like image 502
K Rajitha Avatar asked Oct 27 '25 09:10

K Rajitha


2 Answers

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'.

like image 150
J-Alex Avatar answered Oct 28 '25 23:10

J-Alex


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.

like image 32
Dark Knight Avatar answered Oct 28 '25 23:10

Dark Knight



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!