Send “sigterm” from Java to Bash script
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).
One Solution collect form web for “Send “sigterm” from Java to Bash script”
- Write ‘set -e’ in second line in bash script.
- Dont use trap and while. Replace it by
By this way docker get SIGTERM after java code run end.
#!/usr/bin/env bash set -e someOtherCodeIfNeed exec your_java_code_run