<p>I have a cronjob like below</p>
<pre><code>apiVersion: batch/v1
kind: CronJob
metadata:
  name: foo-bar
  namespace: kube-system
spec:
  schedule: &quot;*/30 * * * *&quot;
  jobTemplate:
    spec:
      template:
        spec:
          serviceAccountName: foo-cleaner
          containers:
          - name: cleanup
            image: bitnami/kubectl
            imagePullPolicy: IfNotPresent
            command:
            - &quot;bin/bash&quot;
            - -c
            - command1;
              command2;
              command3;
            - new_command1;
              new_command2;
              new_command3;
</code></pre>
<p>Sometimes <code>command2</code> fails, throws error and cronjob execution fails. I want to run <code>new_command1</code> even if any command in previous block fails</p>

mbxzxz
<p>In the command section you need to pass the command and args below :</p>
<p>command: [&quot;/bin/sh&quot;,&quot;-c&quot;] args: [&quot;command 1 ||  command 2;  Command 3 &amp;&amp; command 4&quot;]</p>
<p>The command [&quot;/bin/sh&quot;, &quot;-c&quot;] is  to  run a shell, and execute the following instructions. The args are then passed as commands to the shell.</p>
<p>In shell scripting a semicolon separates commands, and &amp;&amp; conditionally runs the following command if the first succeeds, Grep/Pipe (||) runs command1 if it fails then runs command2 also.</p>
<p>As per above command  it always runs command 1 if it fails or gives any error then it continues to run command2.  If command3 succeeds then only it runs command4. Change accordingly in your Yaml and have a try.</p>
<p>Refer this <a href="https://kubernetes.io/docs/concepts/workloads/controllers/job/#writing-a-job-spec" rel="nofollow noreferrer">Doc</a> for cron jobs.</p>
