<p>I am trying to run a pod with 2 containers. Main container executes a docker java image which runs the cron job and the other sidecar(logstash) container reads the log and ship it to the Kibana server. As main container completes once the cron job is finished but sidecar container keeps running as it doesn't gracefully shutdown. I tried <a href="https://github.com/karlkfi/kubexit" rel="nofollow noreferrer">https://github.com/karlkfi/kubexit</a> and preStop container lifecycle hook but didn't work.
Now, I want to try to stop sidecar container using dockerfile. Docker ENTRYPOINT exec java image jar and once java process is finished then I wanted to create / touch a file inside the main container which my other sidecar container looks for that touched file.</p>
<p>This is my current dockerfile</p>
<pre><code>FROM mcr.microsoft.com/java/jre:11-zulu-alpine
ARG projectVersion
WORKDIR /opt/example/apps/sync
COPY /target/sync-app-${projectVersion}.jar app.jar
ENTRYPOINT exec java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar app.jar
</code></pre>
<p>I wanted to add following once the java process execution completes</p>
<pre><code>echo &quot;Going to kill sync-app container...&quot;
trap 'touch /opt/example/apps/sync/container/done' EXIT
</code></pre>
<p>I really wanted to know if it's possible execute the above command once ENTRYPOINT execution is finished? (may be adding these commands together in a script and pass that script to the ENTRYPOINT). But, I am not sure what is required to add those command to execute it properly.</p>
<p>The code I have for the logstash container which would look for the file (which is created by the first container)</p>
<pre><code>- args:
  - |
    dockerd-entrypoint.sh &amp;
    while ! test -f /opt/example/apps/sync/container/done; do
      echo &quot;Waiting for sync-app to finish...&quot;
      sleep 5
    done
    echo &quot;sync-app finished, exiting&quot;
    exit 0
  command:
  - /bin/sh
  - -c
</code></pre>
<p>Please let me know if any more information is required. Thanks for all the help in advance!</p>

Leinz
<p>You can wait for the process to finish by the process PID. The <code>bash</code> interpreter has a build in command called <code>wait</code> where if called with a child PID as argument, it will wait until the child had finished.</p>
<p>So you can create the following entrypoint.sh and add it to your java container.</p>
<p><strong>entrypoint.sh</strong></p>
<pre class="lang-sh prettyprint-override"><code>#!/bin/bash

echo &quot;Cleaning the file signal and running the entrypoint&quot;
rm -rf /opt/example/apps/sync/container/done

exec java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar app.jar &amp;
child_pid=$!
echo &quot;Process started, waiting for the exit&quot;
wait $child_pid

echo &quot;Entrypoint exited, signaling the file&quot;
touch /opt/example/apps/sync/container/done 
</code></pre>
