okieng
Posts: 14
Joined: Thu Apr 18, 2013 4:06 pm

crontab get ip question

Thu May 23, 2013 9:50 pm

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

pepedog
Posts: 1043
Joined: Fri Oct 07, 2011 9:55 am

Re: crontab get ip question

Thu May 23, 2013 11:48 pm

Maybe cron is running without PATH?
ie this needed
/sbin/ifconfig eth0 | grep inet | grep -v inet6 | awk '{print $2}' > ip.txt

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: crontab get ip question

Fri May 24, 2013 7:06 am

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.

okieng
Posts: 14
Joined: Thu Apr 18, 2013 4:06 pm

Re: crontab get ip question

Fri May 24, 2013 12:13 pm

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

Return to “Arch”