LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-18-2007, 07:09 PM   #1
szandor
Member
 
Registered: Jun 2006
Posts: 32

Rep: Reputation: 15
script & cron


well, dyndns.org and an update client are too cumbersome so i wrote a script to check to see if my dynamic ip from my isp has changed. if it has changed, i get an text message with the new ip so i can update my dns and continue to ssh to my box at home even if my ip changes. i have the script executed through cron every 5 minutes. is there a better or alternate way to write the script where it is executed every 5 minutes without using cron? i was thinking of using wait, pause, sleep, etc and chkconfig and something that'll verify the process doesn't die. or should i just go ahead and use cron to exectute the script? here's the script.

DATE=`date +%m-%d-%Y`
TIME=`date +%r`
HOST='somedomain.net'
LFILE='/var/log/dynip/dynip.log'
RCPT='5555555555@cellprovider.net'
REPLY=`/bin/ping -c 1 $HOST | grep 'packets transmitted' | awk '{print $1}'`
DYNIP=`lynx -dump http://whatismyip.org`
DNSIP=`dig +short somedomain.net`

if [ "$REPLY" == "1" ]; then #Continue if the internet is up
if [ $DYNIP != $DNSIP ]; then #Compare Public IP to DNS IP
echo "$DATE: Email sent at $TIME." >> $LFILE
mail -s $DYNIP $RCPT < . &> /dev/null #Email new Public IP to Recipient
else
exit 0 #Exit if Public IP has not changed
fi
fi
 
Old 04-18-2007, 07:34 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
Repetitive tasks are what cron is for - I always use it unless I have a specific reason not to (rare) so I recommend you just schedule it that way.

Was there a specific problem you were having with dyndns? I use it for my home set up with no issues. It has the advantage that I don't need to check my email for an IP address to connect to, I can always just SSH to the domain name.
 
Old 04-18-2007, 07:55 PM   #3
szandor
Member
 
Registered: Jun 2006
Posts: 32

Original Poster
Rep: Reputation: 15
gotcha. well with the dyndns service, i have to use their domain, i.e. kicks-ass.org and cannot use the one i've already registered unless i pay them. also, i have to install a linux update client to update dyndns whenever my ip changes and i would still need to manually update my zone file with the new ip. i just thought i'd created a script to text me the new ip so i'd know immediately when the ip changed so i could update my zone file quicker. testing has come out fine and i added to the script so it will only run if my connection is up. that way if the internet is down, it won't get confused thinking my ip changed.
 
Old 04-18-2007, 11:04 PM   #4
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
Ah - I see what you mean. I don't have the situation here where I need to update my DNS since I'm only authoritative for the stuff behind my firewall.
 
Old 04-19-2007, 06:24 AM   #5
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Just for reassurance: cron is suitable for such a task. Generally I would use cron for task which run with larger intervals than 2-3 minutes, which is true in your case. For shorter intervals sleep might be better.

I have some scripts running in 10 and 20 seconds intervals for which I use sleep. I never have seen any of my scripts dying for no reason, therefor I have not built in a check to see if the process is alive. Of course you have to take care that you don't create a situation in the script which let the script finalize after encountering an error.

jlinkels
 
Old 04-19-2007, 11:11 PM   #6
szandor
Member
 
Registered: Jun 2006
Posts: 32

Original Poster
Rep: Reputation: 15
i see. thanks. i was having a problem with the script erroring out with the first if statment when i simulated the internet being down. it was trying to compare "" with "1" and erroring out. so i added the quotes around $REPLY and which resolved the unary problem and if the internet is down, the if statement completes to the last fi and wala, it exits the nested if.
 
  


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
shell script using /etc/cron.hourly to execute cron.php file? rioguia Programming 3 06-11-2008 08:09 AM
script execution: startup & cron alanvee Linux - Software 3 02-17-2006 01:49 PM
bash script & cron probs. skubik Programming 17 10-16-2005 02:00 PM
PPPD Script problem when cron-scheduling the script andresurzagasti Linux - Networking 0 11-24-2004 02:54 PM
Monthly Archiving Script... help with "date" & "cron" Supp0rtLinux Linux - Software 3 01-03-2003 09:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:40 PM.

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