LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   script works ok manually, but not in cron (https://www.linuxquestions.org/questions/linux-server-73/script-works-ok-manually-but-not-in-cron-4175481630/)

dugave_111 10-21-2013 01:01 PM

script works ok manually, but not in cron
 
Hello,
I'm having a lot of problems with my internet provider. It seems that I get the
IP from their dhcp, but when it runs out, I don't get a single that I my server should request a new one. So in that situation I have a IP on my eth1, but internet doesn't work.

I looked online and found a little script that checks if internet works (google) and if it doesn't it releases ip from eth1 and ask for a new one.

The strange thing is that although the script gets called every 15 minutes (root user) it dons not do the job. If I call it manually, it works immediately and every time.

Here it is:


#!/bin/bash

WGET="/usr/bin/wget"

$WGET -q --tries=10 --timeout=5 http://www.google.com -O /tmp/index.google &> /dev/null
if [ ! -s /tmp/index.google ];then
echo "Internet connectionm down...geting new ip for eth1"
dhclient -r eth1
sleep 5
dhclient eth1
touch /home/yoda/dhcp-new
else
echo "Internet connection ok"
fi




I really don't know what to do and why this is happening.
Any ideas?

I'm running latest ubuntu server.

Thanks!

AlucardZero 10-21-2013 01:32 PM

You probably have to specify the full path to the dhclient command.

ilesterg 10-21-2013 02:14 PM

How does your cron entry looks like?
Code:

crontab -l | grep scriptname
You can do something like this in cron to see the output of the execution:
Code:

/pathblahblah/scriptname > logfile

dugave_111 10-21-2013 02:34 PM

Thanks a lot for the answers!


crontab -l | grep check-internet returns this:


0,15,30,45 * * * * /etc/init.d/check-internet #check internet, get new ip from b-net on eth1 if internet is down
1,16,31,46 * * * * /etc/init.d/check-internet

I added the second line recently trying to get it to work

Since my net connection is now ok, the log from the script just says the echo line: "Internet connection ok"


I'll try the full path to the dhclient, but why is it that it works great when I start the script manually?


Thanks!!

dugave_111 10-21-2013 02:39 PM

It's actually hard to test it out since I need to be in a situation when the internet stops working and it seems it's happening in random intervals. :(
I already e-mailed my provider and they said they were doing a lot of upgrading in my are and that it will be fixed. And it did get better for a period of
time, but it didn't last.

lleb 10-21-2013 03:11 PM

keep in mind that cron runs in its own environment and does not inherit any PATH of the user it is called under.

also please use code flags, select advance to help you, when posting code. makes like much simpler to read...

now i cant say for Ubuntu, but it should be the same. my dhclient resides in /sbin:

Code:

[user@centos Season_1]$ which dhclient
/sbin/dhclient

so in both lines were you have "dhclient" replace it with:

Code:

/sbin/dhclient eth1
dont forget the other line... it might also be a good idea to send the output of several of your lines to some kind of log file.

Code:

LOG=/path/to/log.log
echo "Internet connectionm down...geting new ip for eth1" >> ${LOG}

as an example.

dugave_111 10-22-2013 01:03 PM

Hi!

Well, I added the full path to the dhclient and it seems to work now! Thanks a lot!
I'll see what will be happening in next few days. I added a simple lines that do a log of sorts like this:

Code:

touch /home/some-user-folder/dhcp-released
It's a simple way I added a lot of those in the script so I get a new file each time that the specific part of script gets executed.

I'll report back when I see how it's working now...


All times are GMT -5. The time now is 10:47 PM.