Home > Software engineering >  kubectl command showing empty
kubectl command showing empty

Time:12-17

I have a use case in which I need to run kubectl command in crontab. I am getting empty response from all kubectl commands

I have tried this

#!/bin/sh

/usr/bin/kubectl get po >> /cron.txt

Help will be appreciated!

CodePudding user response:

I don't know why you're not getting any output, but I think I know why it's not telling you why it's not getting output.

Your command line is only going to get stdout, and drop stderr on the floor. It's likely that stderr will tell you why it's not working.

Try changing the command line to:

/usr/bin/kubectl get po 2>&1 >> /cron.txt

CodePudding user response:

Since you've tried running it using bash and sh, the problem is because the ~/.kube/config is in your home.

If you are adding a Cron via crontab -e, you are adding a cron to the root user and root doesn't have the ~/.kube/config in its home.

There are a few ways to fix your problem, see which one is better to you:

  • You can copy the file from ~/.kube/config to /root/.kube/config if for some reason you need to run it using root user, but it is not a good practice.

  • You can set an env var like $KUBECONFIG and export the file location to the whole system. Take a look on configure-access-multiple-clusters/#linux-1.

  • You can also set up a contrab and force your user to run it, to do that you need to edit/create a file in /etc/cron.d/ and pass the username like:

.

0 7 * * * username /usr/bin/kubectl get po >> ~/cron.txt

Let me know if you have any questions.

  • Related