<p>I found this <a href="https://stackoverflow.com/questions/57364453/pass-date-command-as-parameter-to-kubernetes-cronjob">Pass date command as parameter to kubernetes cronjob</a>  which is similar, but did not solve my problem.</p>
<p>I'm trying to backup etcd using a cronjob, but etcd image doesn't have &quot;date&quot; command.</p>
<pre><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: backup
  namespace: kube-system
spec:
  concurrencyPolicy: Allow
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - args:
            - -c
            - etcdctl --endpoints=https://127.0.0.1:2379 --cacert=/etc/kubernetes/pki/etcd/ca.crt
              --cert=/etc/kubernetes/pki/etcd/healthcheck-client.crt --key=/etc/kubernetes/pki/etcd/healthcheck-client.key
              snapshot save /backup/etcd-snapshot-$(DATE_CURR).db
            command:
            - /bin/sh
            env:
            - name: ETCDCTL_API
              value: &quot;3&quot;
            - name: DATE_CURR
              #value: $(date --date= +&quot;%Y-%m-%d_%H:%M:%S_%Z&quot;)
              value: $(date +&quot;%Y-%m-%d_%H:%M:%S_%Z&quot;)
            image: k8s.gcr.io/etcd:3.4.13-0
            imagePullPolicy: IfNotPresent
            name: backup
            resources: {}
            terminationMessagePath: /dev/termination-log
            terminationMessagePolicy: File
            volumeMounts:
            - mountPath: /etc/kubernetes/pki/etcd
              name: etcd-certs
              readOnly: true
            - mountPath: /backup
              name: backup
          - args:
            - -c
            - find /backup -type f -mtime +30 -exec rm -f {} \;
            command:
            - /bin/sh
            env:
            - name: ETCDCTL_API
              value: &quot;3&quot;
            image: k8s.gcr.io/etcd:3.4.13-0
            imagePullPolicy: IfNotPresent
            name: cleanup
            resources: {}
            terminationMessagePath: /dev/termination-log
            terminationMessagePolicy: File
            volumeMounts:
            - mountPath: /backup
              name: backup
          dnsPolicy: ClusterFirst
          hostNetwork: true
          nodeName: homelab-a
          restartPolicy: OnFailure
          schedulerName: default-scheduler
          securityContext: {}
          terminationGracePeriodSeconds: 30
          volumes:
          - hostPath:
              path: /etc/kubernetes/pki/etcd
              type: DirectoryOrCreate
            name: etcd-certs
          - hostPath:
              path: /opt/etcd_backups
              type: DirectoryOrCreate
            name: backup
  schedule: 0 */6 * * *
  successfulJobsHistoryLimit: 3
  suspend: false
</code></pre>
<p>when this runs, it produces a file named &quot;etcd-snapshot-.db&quot;, no date.  If I can catch the logs, it says that &quot;date&quot; is not a known command.  Sure enough, when I am able to exec into the pod while it's running, date does not work in the etcd image.   How can I pass the date from the system as a variable so that it just uses the text and not try to actually invoke the &quot;date&quot; command?</p>
<p>Edit:  Thanks to @Andrew, here is the solution:</p>
<pre><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: backup
  namespace: kube-system
spec:
  concurrencyPolicy: Allow
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - args:
            - -c
            - etcdctl --endpoints=https://127.0.0.1:2379 --cacert=/etc/kubernetes/pki/etcd/ca.crt
              --cert=/etc/kubernetes/pki/etcd/healthcheck-client.crt --key=/etc/kubernetes/pki/etcd/healthcheck-client.key
              snapshot save /backup/etcd-snapshot-$(echo `printf &quot;%(%Y-%m-%d_%H:%M:%S_%Z)T\n&quot;`).db
            command:
            - /bin/sh
            env:
            - name: ETCDCTL_API
              value: &quot;3&quot;
            image: k8s.gcr.io/etcd:3.4.13-0
            imagePullPolicy: IfNotPresent
            name: backup
            resources: {}
            terminationMessagePath: /dev/termination-log
            terminationMessagePolicy: File
            volumeMounts:
            - mountPath: /etc/kubernetes/pki/etcd
              name: etcd-certs
              readOnly: true
            - mountPath: /backup
              name: backup
          - args:
            - -c
            - find /backup -type f -mtime +30 -exec rm -f {} \;
            command:
            - /bin/sh
            env:
            - name: ETCDCTL_API
              value: &quot;3&quot;
            image: k8s.gcr.io/etcd:3.4.13-0
            imagePullPolicy: IfNotPresent
            name: cleanup
            resources: {}
            terminationMessagePath: /dev/termination-log
            terminationMessagePolicy: File
            volumeMounts:
            - mountPath: /backup
              name: backup
          dnsPolicy: ClusterFirst
          hostNetwork: true
          nodeName: homelab-a
          restartPolicy: OnFailure
          schedulerName: default-scheduler
          securityContext: {}
          terminationGracePeriodSeconds: 30
          volumes:
          - hostPath:
              path: /etc/kubernetes/pki/etcd
              type: DirectoryOrCreate
            name: etcd-certs
          - hostPath:
              path: /opt/etcd_backups
              type: DirectoryOrCreate
            name: backup
  schedule: 0 */6 * * *
  successfulJobsHistoryLimit: 3
  suspend: false
</code></pre>

<p>You can use printf like this:</p>
<pre><code>printf &quot;%(%Y-%m-%d_%H:%M:%S_%Z)T\n&quot;
</code></pre>
<p>Use <code>man strftime</code> to get conversion specification sequences.</p>
<p>Just tried it inside etcd container in kubernetes 1.19,</p>
<pre><code>etcdctl --endpoints=https://127.0.0.1:2379 --cacert=/etc/kubernetes/pki/etcd/ca.crt --cert=/etc/kubernetes/pki/etcd/healthcheck-client.crt --key=/etc/kubernetes/pki/etcd/healthcheck-client.key snapshot save /tmp/etcd-snapshot-$(printf &quot;%(%Y-%m-%d_%H:%M:%S_%Z)T\n&quot;).db 
{&quot;level&quot;:&quot;info&quot;,&quot;ts&quot;:1603748998.7475638,&quot;caller&quot;:&quot;snapshot/v3_snapshot.go:119&quot;,&quot;msg&quot;:&quot;created temporary db file&quot;,&quot;path&quot;:&quot;/tmp/etcd-snapshot-2020-10-26_21:49:58_UTC.db.part&quot;}
{&quot;level&quot;:&quot;info&quot;,&quot;ts&quot;:&quot;2020-10-26T21:49:58.760Z&quot;,&quot;caller&quot;:&quot;clientv3/maintenance.go:200&quot;,&quot;msg&quot;:&quot;opened snapshot stream; downloading&quot;}
{&quot;level&quot;:&quot;info&quot;,&quot;ts&quot;:1603748998.7616487,&quot;caller&quot;:&quot;snapshot/v3_snapshot.go:127&quot;,&quot;msg&quot;:&quot;fetching snapshot&quot;,&quot;endpoint&quot;:&quot;https://127.0.0.1:2379&quot;}
{&quot;level&quot;:&quot;info&quot;,&quot;ts&quot;:&quot;2020-10-26T21:49:58.889Z&quot;,&quot;caller&quot;:&quot;clientv3/maintenance.go:208&quot;,&quot;msg&quot;:&quot;completed snapshot read; closing&quot;}
{&quot;level&quot;:&quot;info&quot;,&quot;ts&quot;:1603748998.9136698,&quot;caller&quot;:&quot;snapshot/v3_snapshot.go:142&quot;,&quot;msg&quot;:&quot;fetched snapshot&quot;,&quot;endpoint&quot;:&quot;https://127.0.0.1:2379&quot;,&quot;size&quot;:&quot;5.9 MB&quot;,&quot;took&quot;:0.165411663}
{&quot;level&quot;:&quot;info&quot;,&quot;ts&quot;:1603748998.914325,&quot;caller&quot;:&quot;snapshot/v3_snapshot.go:152&quot;,&quot;msg&quot;:&quot;saved&quot;,&quot;path&quot;:&quot;/tmp/etcd-snapshot-2020-10-26_21:49:58_UTC.db&quot;}
Snapshot saved at /tmp/etcd-snapshot-2020-10-26_21:49:58_UTC.db
</code></pre>
<p>Edit: A reproducible example using jobs, without passing any env vars:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: batch/v1
kind: Job
metadata:
  name: printf
spec:
  template:
    spec:
      containers:
      - name: printf
        image: k8s.gcr.io/etcd:3.4.9-1
        command: [&quot;/bin/sh&quot;]
        args:
        - -c
        - echo `printf &quot;%(%Y-%m-%d_%H:%M:%S_%Z)T\n&quot;`
      restartPolicy: Never
  backoffLimit: 4
</code></pre>
<pre><code># kubectl create -f job.yaml 
job.batch/printf created
# kubectl logs printf-lfcdh 
2020-10-27_10:03:59_UTC
</code></pre>
<p>Notice the use of backticks `` instead of $()</p>
