Send “sigterm” from Java to Bash script

I am starting a Java code from Bash script called 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:

  • operation not permitted Error response from daemon: Cannot start container System error: operation not permitted
  • Vagrant start docker image on existing machine
  • Github repo with more than one python packages
  • Create docker image without source image (OS)
  • How to open Ubuntu GUI inside docker image
  • Docker File: Chmod on Entrypoint Script
  • #!/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: 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-compose error - unable to read file
  • What are the pros and cons of docker data volumes
  • Can I create wheels for python packages on macOS usable for a ubuntu docker image?
  • Docker Service Update Error: EOF
  • Synchronizing numeric user id's between Dockerfiles and docker-compose.yml?
  • Oracle DB+Weblogic in a single container
  • 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.


    #!/usr/bin/env bash
    set -e
    exec your_java_code_run
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.