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:

  • Connect to a VNC inside a docker which is on remote server
  • How to connect to SSHD inside a Docker container from Windows?
  • How to use gpicview inside docker?
  • How to build and run a java instance in Docker
  • Syslog driver in Logstash docker image causing port “timed out” error
  • Deploying Spring WAR to Tomcat-based docker
  • #!/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!

  • Docker error on Windows 2016 “Client.Timeout exceeded while awaiting headers”
  • jenkins pipeline docker build on docker agent
  • Do I need a docker-machine with dlite?
  • Docker engine fillingup storage
  • Pass environment variable from docker to supervisord
  • Run PHPUnit by terminal with Docker
  • 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.