<p>I want to use k8s job to perform some scheduled tasks, as follows:</p>
<pre class="lang-yaml prettyprint-override"><code>containers:
    - name: container-rgs4wl
      image: 'alpine-curl:3.16.0'
      command:
        - |-
          for i in `cat domains`
          do
              http_status=`curl -I -m 10 -o /dev/null -s -w %{http_code} $i`
              curl -X POST -H 'Content-type: application/json' --data  &quot;{\&quot;text\&quot;:\&quot;$i check http status  ${http_status}\&quot;}&quot; ${SLACK_WEBHOOK_URL}
          done
</code></pre>
<p>After completion, it will prompt me the following error:</p>
<pre><code>Error: failed to start container &quot;container-rgs4wl&quot;: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: &quot;for i in `cat domains`；\ndo\n http_status=`curl -I -m 10 -o /dev/null -s -w %{http_code} $i`\n curl -X POST -H 'Content-type: application/json' --data \&quot;{\\\&quot;text\\\&quot;:\\\&quot;$i check http status ${http_status}\\\&quot;}\&quot; ${SLACK_WEBHOOK_URL}\ndone&quot;: stat for i in `cat domains`； do http_status=`curl -I -m 10 -o /dev/null -s -w %{http_code} $i` curl -X POST -H 'Content-type: application/json' --data &quot;{\&quot;text\&quot;:\&quot;$i check http status ${http_status}\&quot;}&quot; ${SLACK_WEBHOOK_URL} done: no such file or directory: unknown
</code></pre>
<hr />
<p>When I specify command in <code>/bin/bash -c</code>:</p>
<pre class="lang-yaml prettyprint-override"><code>containers:
    - name: container-ixi4bh
      image: 'harbor.weex.tech/public/alpine-curl:3.16.0'
      command:
        - /bin/sh
        - '-c'
        - |
          for i in `cat domains` 
          do
              http_status=`curl -I -m 10 -o /dev/null -s -w %{http_code} $i`
              curl -X POST -H 'Content-type: application/json' --data  &quot;{\&quot;text\&quot;:\&quot;$i check http status  ${http_status}\&quot;}&quot; ${SLACK_WEBHOOK_URL}
          done
</code></pre>
<p>It prompts the following in the log:</p>
<pre><code> /bin/sh: syntax error: unexpected word (expecting &quot;do&quot;)
</code></pre>
<blockquote>
<p>When I use the docker run command to execute in a locally started container it works fine.</p>
</blockquote>
<pre class="lang-bash prettyprint-override"><code>$ docker run -ti --rm alpine-curl:3.16.0 sh
/ # for i in `cat domains`
&gt;           do
&gt;               http_status=`curl -I -m 10 -o /dev/null -s -w %{http_code} $i`
&gt;               curl -X POST -H 'Content-type: application/json' --data  &quot;{\&quot;text\&quot;:\&quot;$i check http status  ${http_st
atus}\&quot;}&quot; ${SLACK_WEBHOOK_URL}
&gt;           done
okokokokokokok/ # 
</code></pre>
<blockquote>
<p><strong>ok</strong> means normal return.</p>
</blockquote>
<hr />
<p>well...well...well...Maybe it's an image problem, I replaced it with <code>centos:centos7</code>:</p>
<pre class="lang-yaml prettyprint-override"><code>containers:
        - name: container-x8ba1n
          image: 'centos:centos7'
          command:
            - /bin/bash
            - '-c'
            - |
              for i in `cat domains` 
              do
                  http_status=`curl -I -m 10 -o /dev/null -s -w %{http_code} $i`
                  curl -X POST -H 'Content-type: application/json' --data  &quot;{\&quot;text\&quot;:\&quot;$i check http status  ${http_status}\&quot;}&quot; ${SLACK_WEBHOOK_URL}
              done
</code></pre>
<p>There are still exceptions, as follows:</p>
<pre><code> /bin/bash: -c: line 1: syntax error near unexpected token `http_status=`curl -I -m 10 -o /dev/null -s -w %{http_code} $i`'
 /bin/bash: -c: line 1: `    http_status=`curl -I -m 10 -o /dev/null -s -w %{http_code} $i`'
</code></pre>
<p>Hey...I really don't know the reason, but I finally wrote a shell script and mounted it into the container for execution, this is ok.</p>
<pre class="lang-yaml prettyprint-override"><code>          containers:
            - name: container-u37d3w
              image: 'centos:centos7'
              command:
                - /bin/bash
              args:
                - /check.sh
              env:
                - name: SLACK_WEBHOOK_URL
                  value: &gt;-
                    https://hooks.slack.com/services/...
              resources: {}
              volumeMounts:
                - name: volume-m3lym7
                  readOnly: true
                  mountPath: /domains
                  subPath: domains
                - name: volume-ho9ctd
                  readOnly: true
                  mountPath: /check.sh
                  subPath: check.sh
</code></pre>
<p>I would like to know what is the difference between using <code>command</code> or <code>args</code> and executing a shell script.</p>

Jason Tom
<p>You can consider it like, <strong>command</strong> is like the <strong>command</strong> we run in Linux, for example, <strong>printenv</strong>, <strong>grep</strong>, <strong>echo</strong> and you pass the <strong>argument</strong> into it that's <strong>args</strong>.</p>
<p>like <code>echo(command) &quot;hello&quot;(args)</code></p>
<p>So if you want to run the command in shell you can try something like</p>
<pre><code>command: [&quot;/bin/sh&quot;]
args: [&quot;-c&quot;, &quot;while true; do echo hello; sleep 10;done&quot;]
</code></pre>
<p>Or else you can try something like</p>
<pre><code>  image: k8s.gcr.io/busybox
  command: [ &quot;sh&quot;, &quot;-c&quot;]
  args:
  - while true; do
      echo -en '\n';
      printenv MY_NODE_NAME MY_POD_NAME MY_POD_NAMESPACE;
      sleep 10;
    done;
  env:
</code></pre>
<p>You can read more about : <a href="https://kubernetes.io/docs/tasks/inject-data-application/_print/" rel="nofollow noreferrer">https://kubernetes.io/docs/tasks/inject-data-application/_print/</a></p>
