crontab无法执行kubectl命令
问题:自己写了个kubectl命令删除失败pods的脚本,然后crontab自动执行总是失败。 分析: [root@k8s-master-1 lyf]# which kubectl /usr/local/bin/kubectl [root@k8s-master-1 lyf]# cat /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root [root@k8s-master-1 lyf]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin 可以发现cron的环境变量少了/usr/local/bin /usr/local/sbin/ 以及/root/bin 解决: 在脚本里加入一行 export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin 即可
#!/usr/bin/bash export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin #for i in `kubectl get pods -n km145 | grep manage-deploy | awk '{print $1}'`;do kubectl delete pods $i -n km145 --force ;done for i in `kubectl get pods -n pre | grep manage-deploy | awk '{print $1}'`;do kubectl delete pods $i -n pre;done #for i in `kubectl get pods -n vip | grep manage-deploy | awk '{print $1}'`;do kubectl delete pods $i -n vip --force ;done
标签:bin,kubectl,sbin,root,crontab,命令,usr,local From: https://www.cnblogs.com/ruiy/p/18027587