LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 10-21-2013, 01:01 PM   #1
dugave_111
LQ Newbie
 
Registered: Oct 2006
Posts: 24

Rep: Reputation: 0
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!
 
Old 10-21-2013, 01:32 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
You probably have to specify the full path to the dhclient command.
 
Old 10-21-2013, 02:14 PM   #3
ilesterg
Member
 
Registered: Jul 2012
Location: München
Distribution: Debian, CentOS/RHEL
Posts: 587

Rep: Reputation: 72
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
 
Old 10-21-2013, 02:34 PM   #4
dugave_111
LQ Newbie
 
Registered: Oct 2006
Posts: 24

Original Poster
Rep: Reputation: 0
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!!
 
Old 10-21-2013, 02:39 PM   #5
dugave_111
LQ Newbie
 
Registered: Oct 2006
Posts: 24

Original Poster
Rep: Reputation: 0
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.
 
Old 10-21-2013, 03:11 PM   #6
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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.
 
Old 10-22-2013, 01:03 PM   #7
dugave_111
LQ Newbie
 
Registered: Oct 2006
Posts: 24

Original Poster
Rep: Reputation: 0
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...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script runs Manually but not in Cron, yet other parts of the script run mccartjd Linux - Newbie 5 01-08-2012 06:54 PM
Script runs manually but not from cron redvelo Linux - Newbie 6 09-29-2010 01:37 PM
[SOLVED] Works manually, but not w/ cron, quotation problem bryanvick Linux - Software 6 10-21-2009 10:00 AM
cron job doesnt work, it works manually scofiled83 Programming 8 05-30-2009 03:12 PM
Script works when run manually but not in cron job Harlin Linux - Software 7 10-16-2007 06:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 04:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration