LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Fedora
User Name
Password
Fedora This forum is for the discussion of the Fedora Project.

Notices

Reply
 
LinkBack Search this Thread
Old 04-21-2007, 02:23 PM   #1
farpoint
Member
 
Registered: Sep 2004
Location: France
Distribution: Debian Sarge, Etch. FC1,2,3,4,5. Slackware 10.0. Gentoo. Kubuntu.
Posts: 193

Rep: Reputation: 30
Script wanted to start ntpd when dialup connection is up


This is the place to come when you want problems solved, so here goes.

My dial up conection is by serial modem connected to a smoothwall firewall on an old machine. I have 2 machines on the LAN. One, machine A is configured to get it's time using ntpd from the Internet via servers setup in /etc/ntp.conf, but I'm having problems with an FC2 install. I boot up with no Internet connection, then by means of the web interface to the Smoothwall make the dialup connection. Running /usr/sbin/ntpq I get varying output. Sometimes just one of the 3 timerservers, sometimes none. If I stop, and restart ntpd when the Internet connection is up, I consistently see all 3 Internet time servers.

What I'd like is a script that will detect when an Internet connection is available on machine A, then run /etc/init.d/ntpd start.

Machine B on the LAN isn't a problem. It gets it's time from machine A.

btw. I also have FC5, and FC6 on machine A, and ntpd seems to work ok with these, even when an Internet connection isn't initially available.

I would like to resolve the problem on FC2 though, as I still use it daily for all my email.

Many thanks in advance for a suitable script.

Nigel. aka farpoint.
 
Old 04-25-2007, 09:53 AM   #2
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 535Reputation: 535Reputation: 535Reputation: 535Reputation: 535Reputation: 535
Code:
#!/bin/sh
while true;
do
sleep 15;
pppon= echo `ifconfig | grep ppp0`;
ntprunning= echo `ps ax | grep ntpd`; 
if [ "$pppon" == 'ppp*' ] ; then
  if [ "$ntprunning" != 'ntpd*' ] ; then
     if [ -x /etc/init.d/ntpd ]; then
       /etc/init.d/ntpd restart >/dev/null 2>&1
     fi
  fi
else
  if [ "$ntprunning" == 'ntpd*' ] ; then
    if [ -x /etc/init.d/ntpd ]; then
      /etc/init.d/ntpd stop >/dev/null 2>&1
    fi
  fi
endif
done
Since your post has been unreplied to for a bit, I figured I'd give this s try..
DISCLAIMER: I'm new to Linux, and far from skilled with shell scripting!!
The code above is a variant of the daemon I use to monitor for a new connection, and if one exists, then create an ethernet bond between two NICs.
I have changed it to reflect YOUR needs as I understand them.
I'm certain that this is probably a VERY primitive and/or clumsy way of doing things, however it worked perfectly for what I wanted it to do.
The code will need editing almost certainly, especially the line using 'PS AX' because I haven't much used 'PS' so I did a quick test of PS in a console and got 'something' returned to me, and I did..
So all this does in the end, as it is written here, is query IFCONFIG every 15 seconds looking for a connection ppp0 to be existing, and query running processes looking for ntpd. If ppp0 exists and there is no ntpd process, it starts ntpd.
Conversely, if there is NO ppp0 existing, but there IS a ntpd process, it stops the ntpd.

If anything, I hope this can help you figure out a better way, or even get this working to your satisfaction
To run this as a daemon, start it from a script during boot with a command like: /etc/filename-you-chose & (the & symbol causes bash to fork the process into the background as a daemon, and system resource usage is unmeasurable.)
 
Old 04-25-2007, 03:56 PM   #3
farpoint
Member
 
Registered: Sep 2004
Location: France
Distribution: Debian Sarge, Etch. FC1,2,3,4,5. Slackware 10.0. Gentoo. Kubuntu.
Posts: 193

Original Poster
Rep: Reputation: 30
Apologies. I left the question for a couple of days, and also posted then to the ntp, and the fedora list.

I have now resolved the problem. The script you posted may well have worked for me if the modem had been on machine A, but the modem is on a dedicated machine with the Smoothwall Express2 firewall on it. I have to boot one of the 2 machines on the LAN, so as to get access to the web interface for the Smoothwall. Only then can I make the Internet connection. So it goes like this. Smoothwall is up, and running, then boot up machine A on the LAN.Ntpd is running, and trying to find Internet time servers, but can't because there is, as of yet no Internet connection. Bear in mind this is with FC2, and FC5/6 seem to handle this ok. Anyway, ntpd on FC2 appears to give up trying, go out to lunch, or whatever, so that when the Internet connection eventually comes up there are varying results when running /usr/sbin/ntpq, none of which are showing all 3 timeservers. Sometimes just one timeserver, othertimes "No association ID's returned".

Here is the fix for anyone interested.

I first tried the script in the ntp-4.2.4p0 tarball named ntp-restart. Changed the sleep time from 10 to 420, and added it to etc/rc.d/rc.local as.
/usr/local/bin/ntp-restart &

The "&" as I only recently found out from someone on the Fedora list allows the script to run in the background, and allows the bootup to continue uninterrupted. That's my lack of scripting knowledge for you!

This works ok, but I had to be by the machine when I booted it, as otherwise sleep 420 would run out of time, and restart the ntp daemon, and still with no Internet connection I'd be back to square 1.

So I asked on the ntp list for a script that would ping an Internet server at 60 sec intervals, and when it got a positive response would run the ntp-restart script. It was pinging my webserver by the way, only with a count of 4.

Steve Kostecke at ntp kindly provided one, as below.

#!/bin/sh

# The hostname or IP address of the host that you want to ping.
TARGET=

while [ 1 ]
do
/bin/ping -c 4 $TARGET 2>/dev/null >/dev/null && break
/bin/sleep 60
done

/usr/local/bin/ntp-restart

So I name this as "Ping-to-start-ntpd &", and put it in /etc/rc.d/rc.local, removing the ntp-restart line from rc.local. Go to /usr/local/bin/ntp-restart, and change the sleep from 420 back to it's default 10.

Reboot machine A with no Internet connection. The script runs from rc.local, then having gone for a coffee uptairs while it's booting, I return and by means of the Smoothwalls web interface, connect to the Internet, and then run ntpq> pe, and all 3 Internet timeservers are there. Job done.

Sorry for the long post, but the info might be usefull to someone else.

Nigel. aka farpoint.
 
Old 04-25-2007, 04:03 PM   #4
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 535Reputation: 535Reputation: 535Reputation: 535Reputation: 535Reputation: 535
Nice Well undoubtedly you have gotten a far more "correct" solution as laid out there than I created. Either way, glad you got a solution at all, and thanks for the update --- it looks like something other people will appreciate knowing about.
SV
 
  


Reply

Tags
ntpd, startup


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I need help with a dialup connection tcon50 Suse/Novell 1 11-24-2006 05:15 PM
script for restarting ntpd in /home/user/.kde/autostart farpoint Fedora 2 12-12-2005 02:46 PM
Wanted to start C in UNIX from scratch AkshatBhandari Linux - Newbie 1 03-19-2005 12:32 PM
NTPD - add to rc.local or write a script downinthemine Linux - Software 0 04-21-2004 10:52 AM
script wanted ust Linux - General 2 09-03-2003 02:20 PM


All times are GMT -5. The time now is 01:35 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration