<p>I'm trying to run a pod-bootstrapping script (which basically runs a java program) after tomcat is started in the pod. My pod runs a single tomcat-based container. Here is the snippet that I'm working and re-working on:</p>

<pre><code>     - image: myreg/mydir/myimg:local
       name: tomcat-adminapp
       command: ["/bin/sh"]
       args: ["-c", "$CATALINA_HOME/bin/startup.sh &amp;&amp; /scripts/runUpg.sh"]
</code></pre>

<p>runUpg.sh script:</p>

<pre><code>#!/bin/sh
ls -l $CATALINA_HOME/webapps/app/WEB-INF/lib/
chmod 755 $CATALINA_HOME/webapps/app/WEB-INF/lib/*
java -classpath "$CATALINA_HOME/webapps/app/WEB-INF/lib/*" com.myclass.Upg &lt;args&gt; 
tail -f /dev/nul
</code></pre>

<p>It is starting tomcat but my script execution fails. When I exec into the pod, I see the war is exploded, CATALINA_HOME is set and tomcat process is running. But I see following script errors in kubectl logs:</p>

<blockquote>
  <p>ls: cannot access '/userhome/tomcat/webapps/app/WEB-INF/lib/': No such file or directory
  chmod: cannot access '/userhome/tomcat/webapps/app/WEB-INF/lib/*': No such file or directory
  Error: Could not find or load main class com.myclass.Upg
  Caused by: java.lang.ClassNotFoundException: com.myclass.Upg</p>
</blockquote>

<p>From the error I'm guessing the shell script might not be waiting for tomcat to finish its startup. I'm not sure why.. </p>

<p>When I manually run the script from inside the pod, it runs fine.</p>

<p>Any pointers here are appreciated..</p>

<p><strong>Update:</strong> I confirmed it is indeed the timing issue. When I use <code>$CATALINA_HOME/bin/startup.sh &amp;&amp; sleep 60 &amp;&amp; /scripts/runUpg.sh</code>, the script seems to run fine. But I think this is hack-ish and not the right way to achieve a call to a class within the webapp. </p>

A.R.K.S
<p>Consider postStart hook to execute a script after the container is started.</p>

<p>sample reference</p>

<pre><code>
apiVersion: v1
kind: Pod
metadata:
  name: lifecycle-demo
spec:
  containers:
  - name: lifecycle-demo-container
    image: nginx
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh", "-c", "echo Hello from the postStart handler &gt; /usr/share/message"]
</code></pre>
