title: How can I run a cron command with environmental variables set by kubernetes secret
body: <p>I'm using kubernetes secret as my environment variable
  (<a href="http://kubernetes.io/docs/user-guide/secrets/#using-secrets-as-environment-variables" rel="nofollow noreferrer">http://kubernetes.io/docs/user-guide/secrets/#using-secrets-as-environment-variables</a>).</p>
  
  <p>I've check whether env vars set correctly by </p>
  
  <pre><code>kubectl exec -it my_pod bash
  </code></pre>
  
  <p>1.</p>
  
  <pre><code>echo $RAILS_ENV #=&gt; staging
  </code></pre>
  
  <p>2.</p>
  
  <pre><code>bin/rails c;
  puts ENV['RAILS_ENV'] #=&gt; staging
  </code></pre>
  
  <p>It works fine for my rails application and bash command, but doesn't work when executing cron process.</p>
  
  <p>I've read some post to understand cron process (e.g. <a href="https://unix.stackexchange.com/questions/27289/how-can-i-run-a-cron-command-with-existing-environmental-variables">https://unix.stackexchange.com/questions/27289/how-can-i-run-a-cron-command-with-existing-environmental-variables</a>) and know that I should source .profile or .bashrc before executing cron command like.</p>
  
  <pre><code>$ crontab -l
  
  * * * * * . $HOME/.profile; /bin/bash -l -c 'echo rails_env=$RAILS_ENV &gt;&gt; /tmp/cron_test.log'
  
  * * * * * . $HOME/.bashrc; /bin/bash -l -c 'echo rails_env=$RAILS_ENV &gt;&gt; /tmp/cron_test.log'
  </code></pre>
  
  <p>but both doesn't work for me ˊˋ</p>
  
  <p>result</p>
  
  <pre><code>rails_env=
  </code></pre>
  
  <p>Any help will be appreciated!</p>
  
  <p>===</p>
  
  <p><strong>[update: add kubernetes yaml config]</strong></p>
  
  <p>Both environment variables assign by kubernetes config and kubernetes secret can read in bash and rails application, but doesn't work in cron process.</p>
  
  <p>deployment.yaml</p>
  
  <pre><code>spec:
    containers:
    - name: my_rails
    ...
      command:
      - bash
      args:
      - /tmp/run_cron.sh
    ...
      env:
        - name: RAILS_ENV
          value: staging
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: rails-secret
              key: MYSQL_PASSWORD
  </code></pre>
  
  <p>secret.yaml</p>
  
  <pre><code>apiVersion: v1
  kind: Secret
  metadata:
    name: rails-secret
  type: Opaque
  data:
    MYSQL_PASSWORD: ENCODED_PASSWORD
  </code></pre>