I have below small python script
import os
os.system('kubectl get pods --context students-cmn')
when i run this command manually from terminal it is working , no issue , so i configured it to run as a cron job , but when cron job triggered getting below error
sh: kubectl: command not found
why , when the cronjob triggered , kubectl not working ?
can anyone please help
i got the same issue when i tried to execute "kubectl" command into .sh/py file from CentOS 7 with cron. i could run script .sh/.py file manually and the output was fine. when i tried to set cron job with script, "kubectl" command could not be executed. it didnot generate any logs for cron events. if you dont mind, please support me.
crontab : * * * * * python /home/sanglv/kube-status.py >> /tmp/status.txt
kube-status.py file :
#!/usr/bin/env python
#ls command
import os
os.system('/usr/bin/kubectl get pods --all-namespaces --kubeconfig="/root/.kube/config"')
First of all, I imagine you are planning on adding code to your python script and that that is why you use python. I assume you used the crontab of the user that can run the command.
When you execute a command in cron
you must specify the full path to the command. To find the full path to kubectl
, you issue the following in Terminal:
which kubectl
It will print the full path.
Then, you edit your script (assuming the full path is "/opt/Kubernetes/bin"):
import os
os.system('/opt/Kubernetes/bin/kubectl get pods --context students-cmn')