简体   繁体   English

ip命令无法在crontab的shell脚本中获取IP地址

[英]ip command could not get IP address in shell script in crontab

I use Cloudflare API script to update DDNS on my Raspberry PI using crontab. 我使用Cloudflare API脚本使用crontab更新我的Raspberry PI上的DDNS。 The shell script works fine in Debian but fails in CentOS/Fedora. shell脚本在Debian中工作正常但在CentOS / Fedora中失败。 While run in the terminal, it works. 在终端中运行时,它可以工作。

I checked out the ip addr could not get data, but I could not solve it. 我检查了ip addr无法获取数据,但我无法解决它。 And I tried out that I can instead ip addr with hostname -I, then it works well. 我尝试了我可以用主机名-I替换ip addr,然后它运行良好。

But I am wondering why ip could not work in .sh / bash shell script? 但我想知道为什么ip无法在.sh / bash shell脚本中工作?

Ferora 28 server Raspberry. Ferora 28服务器Raspberry。 I tried many resolvation I can googled, none works. 我尝试过多次解决方案,我可以使用Google搜索,但没有效果。

#!/bin/bash
#this works
ip=$(hostname -I | awk '{print $NF;exit}')
echo $ip>>/usr/local/bin/cloudflare.log 

#this fail
ips=$(ip route get 1:: | awk '{print $(NF-4);exit}')
echo $ips>>/usr/local/bin/cloudflare.log 

# crontab -l
#automatic update ddns per 1 min
* */1 * * *  /usr/local/bin/cf-ddns.sh >/dev/null 2>&1

cat cloudflare.log
xx.xx.xxx.xx
<Blank_None>

crontab dosn't set PATH an cannot find the binarys. crontab dos not set PATH一个找不到binarys。 Add PATH at the top of your script, or with an export at top of crontab. 在脚本顶部添加PATH,或在crontab顶部导出。

# for example
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM