0

I am starting a Java code from Bash script called start.sh. The Bash script fires up the Java code and then the Java code runs. At the end of the Java program, I want to send a signal back to the Bash script to terminate. Keep in mind that the Bash script runs with PID = 1. And I have to kill the PID 1 process.

I have the bash script set up such that it runs in an infinite loop and waits for a termination signal:

#!/bin/bash

# Run the java code here..

# Listen for an exit command.

trap 'exit 0' SIGTERM
while true; do :; done

I am using Docker instances and the signal is sigterm. I am following this tutorial: https://www.ctl.io/developers/blog/post/gracefully-stopping-docker-containers/ which explains how to send a sigterm from command line. I want to automate this process and close/kill the docker instance from inside as soon as the Java program ends.

How do I send the signal back to the bash script that started the Java code in the first place?

Should I follow this method to send a signal as arguments to the bash script? Or will it start another bash script with a different PID (not 1).

Help needed!

4
  • use kill -15 PID Commented Apr 20, 2016 at 10:19
  • where? In the bash script? Commented Apr 20, 2016 at 10:21
  • No sorry, read it the wrong way round, as in send it to java from bash. Commented Apr 20, 2016 at 10:23
  • The thing is, I can terminate (send sigterm) easily from outside to the docker instance. lt terminates as required. I just wanna send it from Java and automate it so I don't have to write any sigterm command and keep checking if the java program has finished or not Commented Apr 20, 2016 at 10:26

1 Answer 1

0
  1. Write 'set -e' in second line in bash script.
  2. Dont use trap and while. Replace it by 'exec your_java_code_run'.

By this way docker get SIGTERM after java code run end.

Exemple:

#!/usr/bin/env bash
set -e

someOtherCodeIfNeed

exec your_java_code_run
Sign up to request clarification or add additional context in comments.

3 Comments

something like this: #!/bin/bash set -e # Run the java code here.. exec HelloWorld ?
#!/usr/bin/env bash set -e someOtherCode exec HelloWorld
When your_java_code_run finish conteiner will die =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.