Page 1 of 1

crontab get ip question

Posted: Thu May 23, 2013 9:50 pm
by okieng
Hi,
My goal is find and write ip address with crontab

here is my script

findip.sh
#!/bin/bash
echo "myip:"
ifconfig eth0 | grep inet | grep -v inet6 | awk '{print $2}' > ip.txt

here is cron (create : crontab -e)
*/1 * * * * /root/findip.sh

I am run findip.sh with ssh terminal. Everythings working. I saw ip address ip.txt
myip
192.168.0.111

But whan ip.txt create with cron there is no ip. address only
myip:

what is the problem?

Thanks for help.

Oki

Re: crontab get ip question

Posted: Thu May 23, 2013 11:48 pm
by pepedog
Maybe cron is running without PATH?
ie this needed
/sbin/ifconfig eth0 | grep inet | grep -v inet6 | awk '{print $2}' > ip.txt

Re: crontab get ip question

Posted: Fri May 24, 2013 7:06 am
by sprinkmeier

Code: Select all

hostname --all-ip-addresses | xargs --max-args 1 | grep --invert-match :
nothing wrong with your solution but it's always handy to know an extra way to skin a cat.

consider writing to /dev/shm/ip.txt. this is a RAM filesystem and won't wear out your SD card.

Re: crontab get ip question

Posted: Fri May 24, 2013 12:13 pm
by okieng
Thanks Guys,
Pepedog your code is works.
Actually i just try */usr/bin/ifconfig ... buy this was not work.
But */sbin/ifconfig ... is working .

Thanks again

Oki