user526206
user526206

Reputation:

How to start java process if it die from script

I have bash script to start java process using below command:

#!/bin/bash
java -jar test.jar

I want to make a script so that if the java process of this jar die it will start again from this script. Do i need a do while loop in my script and what condition should i use to check that is java process of this jar is running or not and if die then fire it up again.

Upvotes: 1

Views: 416

Answers (2)

Andreas Louv
Andreas Louv

Reputation: 47099

The following will run until test.jar exits with 0 as exit code

while ! java -jar test.jar; do :; done

Breakdown:

while ! java -jar test.jar; do :; done
#     ^ ^                      ^
#     | |                      while loops needs a body, and `:` will do nothing
#     | The command to run, while will check the exit code and continue if its 0 (true) 
#     Flip the exit code, so true becomes false and false becomes true

If you are interested to run until test.jar exits will falsy exit code, you can omit the !:

while java -jar test.jar; do :; done

Upvotes: 1

Inian
Inian

Reputation: 85560

Use the bash until in a loop until the bash success return code 0 is seen.

#!/bin/bash
until java -jar test.jar
do
   echo "Retrying Command.."
done

Upvotes: 1

Related Questions