Script wanted to start ntpd when dialup connection is up
FedoraThis forum is for the discussion of the Fedora Project.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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.
#!/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.)
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.