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:

  • run mysql:5.5 docker image from ansible
  • Error when running retrain.py during retraining for image classifier based on tensorflow for poets
  • docker current not installed - bash on ubuntu on windows 10
  • Why docker login command saves unencrypted password on my computer?
  • Elastic Beanstalk docker error
  • Dockerfile COPY takes a stale copy of the file?
  • #!/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!

  • What is the benefit of convoy-glusterfs over pure glusterfs
  • Hadoop (yarn) control resource consumption (CPU and RAM), on docker
  • AWS S3 Elastic Beanstalk Docker - /dev/fus: Operation not permitted
  • Docker Sidekiq Starting - Rails App with Puma
  • kafka monitoring using JMX when running kafka in docker in boot2docker
  • marathon & docker - why is marathon trying to execute docker image continuosly?
  • One Solution collect form web for “Send “sigterm” from Java to Bash script”

    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
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.