crontab 获取本机ip

写了个shell获取ip的函数,如下

function GetLocalIP() {
  ifconfig  | grep 'inet '| grep -v '127.0.0.1' |
  cut -d: -f2 | awk '{print $1}' |head -n1
}

 添加到crontab中执行,发现得到的ip_address为空。

这是由于

crontab中的环境变量与用户环境的环境变量不一样,因其不会从缺省的用户profile文件中读入环境变量参数,最简单的方法是用source命令(.)

如 . /etc/profile; bash -x xxx.sh 即可。

 上述方法无效。

在脚本中,指定ifconfig的全路径即可,ubuntu下为/sbin/ifconfig

 

posted @ 2013-07-24 14:59  westfly  阅读(567)  评论(0)    收藏  举报