You are not logged in.

#1 2012-04-09 17:13:38

LordOfInsomnia
Member
Registered: 2011-10-13
Posts: 69

[SOLVED]Cron + bash script problem

I have made bash script which checks ip address, and if ip address had change since last time it sends email, restartrs mpd...
It all works perfect when if started from terminal if it is called with root account.

When I try to use script with cron in 90% of cases it does not recognize ip address, mail could not be sent...

This is the script:
/usr/sbin/checkIp

#!/bin/sh

echo loading>/home/lordofinsomnia/ipTest.txt

curIPFile=/home/lordofinsomnia/ip.txt
IPFunc=$(ip addr | grep inet | grep eth0)
SubStr=${IPFunc:9:15}
SubStrIndex2=$(expr index "$SubStr" /)
curIP=${IPFunc:9:SubStrIndex2-1}

oldIP=$(cat $curIPFile)
echo curIP:$curIP>>/home/lordofinsomnia/ipTest.txt

echo before if>>/home/lordofinsomnia/ipTest.txt

if [ curIP == oldIP ]; then
	#echo ip is unchanged
	mailSubject="ip address test"
	mail -s "$mailSubject" "mail@mail.com" < /dev/null	
else
	#echo ip is changed
	confReplace=ipaddress
	sed "s/$confReplace/$curIP/g" /home/lordofinsomnia/mpd1.conf > /etc/mpd.conf
	rc.d restart mpd	
	mailSubject="ip address change"	
	echo $curIP>/home/lordofinsomnia/ip.txt
	mail -s "$mailSubject" "mail@mail.com" < /home/lordofinsomnia/ip.txt
	echo sent mail>>/home/lordofinsomnia/ipTest.txt
fi

Cron setting is:

*/5 * * * * root /usr/sbin/checkIp

I have tried,
Cron setting is:

*/5 * * * * /usr/sbin/checkIp

but it results are the same... hmm

my "log" file has output like this. IP is not detected...

loading
curIP:
before if
sent mail

What am I doing wrong?

Last edited by LordOfInsomnia (2012-04-09 21:28:08)

Offline

#2 2012-04-09 17:35:08

brebs
Member
Registered: 2007-04-03
Posts: 3,742

Re: [SOLVED]Cron + bash script problem

Debug it more. Log the output of "ip addr".

Maybe /usr/sbin/ (location of "ip") isn't specified in PATH in /etc/crontab.

Offline

#3 2012-04-09 17:42:07

bohoomil
Member
Registered: 2010-09-04
Posts: 2,376
Website

Re: [SOLVED]Cron + bash script problem

<disclaimer>
Ignore if I don't get something correctly...
</disclaimer>

To get only the current local IP, I'd do:

ip addr | awk '/wlan0$/ {print $5}'

Of course, change the network interface so it refers to your setup.


:: Registered Linux User No. 223384

:: github
:: infinality-bundle+fonts: good looking fonts made easy

Offline

#4 2012-04-09 17:51:35

LordOfInsomnia
Member
Registered: 2011-10-13
Posts: 69

Re: [SOLVED]Cron + bash script problem

@brebs

I have tried it and loged it to a file it's loged in file, but rest of code is not working.

@bohoomil
I don't have wireless

ip addr | awk '/eth0$/ {print $2}'

this works for my lan, but still needs parsing...

Offline

#5 2012-04-09 17:53:32

bohoomil
Member
Registered: 2010-09-04
Posts: 2,376
Website

Re: [SOLVED]Cron + bash script problem

It should work, but you may have to change it accordingly to isolate the IP. Can you paste the exact output of your 'ip addr'?


:: Registered Linux User No. 223384

:: github
:: infinality-bundle+fonts: good looking fonts made easy

Offline

#6 2012-04-09 18:00:48

LordOfInsomnia
Member
Registered: 2011-10-13
Posts: 69

Re: [SOLVED]Cron + bash script problem

My script works, but this is output.

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether xx:yy:zz:aa:bb:cc brd ff:ff:ff:ff:ff:ff
    inet 188.x.y.z/22 brd 188.x.y.255 scope global eth0
    inet6 xx::yy:zz:aa:bb36/64 scope link 
       valid_lft forever preferred_lft forever

this part od code works perfect, from terminal...

IPFunc=$(ip addr | grep inet | grep eth0)
SubStr=${IPFunc:9:15}
SubStrIndex2=$(expr index "$SubStr" /)
curIP=${IPFunc:9:SubStrIndex2-1}

Offline

#7 2012-04-09 19:00:12

brebs
Member
Registered: 2007-04-03
Posts: 3,742

Re: [SOLVED]Cron + bash script problem

Why did you lazily ask us, rather than just continue debugging?

$ IPFunc=$(echo $(ip addr | grep inet | grep eth0) | cut -d" " -f2)
$ echo $IPFunc
192.168.1.2/24

Your "9" should have made you suspicious. That ip command is using a weird delimiter, which "echo" converts into a manageable space.

Offline

#8 2012-04-09 21:27:02

LordOfInsomnia
Member
Registered: 2011-10-13
Posts: 69

Re: [SOLVED]Cron + bash script problem

I have added another cut to work smile
cut -d "/" -f

curIP=$(echo $(ip addr | grep inet | grep eth0) | cut -d" " -f2 | cut -d "/" -f1)

I did not know for cut command smile

Thank You! smile

Offline

#9 2012-04-09 21:38:48

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: [SOLVED]Cron + bash script problem

Or, if you would prefer something simpler:

awk '/eth0$/ {print $2}' <(ip addr)

if you don't want the netmask

awk '/eth0$/ {sub(/\/24/,""); print $2}' <(ip addr)

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

Board footer

Powered by FluxBB