# NTP sync time possible after connecting with NetworkManager?

## Dirk.R.Gently

.

This is a little problem I hoping to iron out.  I'm not using the net.? scripts to start wireless anymore at boot, instead the last few weeks I've been using NetworkManager.  I've been using and didn't realize that using it will null my ntp-client startup script.  I didn't realize how handy it has been to sync with a time servers when I boot until i started being late to a few events.   :Rolling Eyes:   I can obviously start is manually, but I was hoping for an automated way.  Don't some ntp clients persistently check, maybe I can install one of those?

----------

## ichbinsisyphos

 *Dirk.R.Gently wrote:*   

> .
> 
> This is a little problem I hoping to iron out.  I'm not using the net.? scripts to start wireless anymore at boot, instead the last few weeks I've been using NetworkManager.  I've been using and didn't realize that using it will null my ntp-client startup script.  I didn't realize how handy it has been to sync with a time servers when I boot until i started being late to a few events.    I can obviously start is manually, but I was hoping for an automated way.  Don't some ntp clients persistently check, maybe I can install one of those?

 

when does NetworkManager start? with X? or manually?

when X goes down, your internet connection is off?

well you could sync regularly with a cron script.

----------

## Dirk.R.Gently

NetworkManager starts with X, it does 't use the net. scripts at the boot process, so I need to find a way to sync with the time server after X loads.

----------

## ichbinsisyphos

well you could start a script with X, if you put it in .xinitrc in your home folder.

let it check if networkmanager is running:

```
/bin/ps aux | grep --invert-match grep | grep networkmanager
```

whatever the actual process name is.

if the init-script makes problems, you can execute

```
ntpdate -s -b -u server
```

directly

----------

## Dirk.R.Gently

.

 *ichbinsisyphos wrote:*   

> well you could start a script with X, if you put it in .xinitrc in your home folder.
> 
> let it check if networkmanager is running:
> 
> ```
> ...

 

I appreciate the help ichbinsisyphos.  I'm kinda new to scripting so would putting something like this be safe?

```
/bin/ps aux | grep --invert-match grep | grep nm-applet

sleep 9

/etc/init.d/ntp-client start
```

Is /bin/ps a list of processes that are running?  Hmm, even if network manager is running it wont mean its connected.  Maybe I can grep iwconfig somehow...

Actually I think I found something easier:

If I allow ntpdate to run as non-root, and make sure this line is in the config:

```
nano /etc/conf.d/ntpd

NTPD_OPTS="-u ntp:ntp"
```

/usr/sbin isn't in the regualr users bash file for executables so I linked ntpdate to /usr/bin

 ln -s /usr/sbin/ntpdate /usr/bin

Ok, I'm rambling.  I just read the man page for ntpdate and it looks like ntpdate can succesfully be used in cron if you can't use the daemon.  It says:

When running ntpdate on a regular basis from cron as an alternative to running a daemon, doing so once every hour or two will result in precise enough timekeeping to avoid stepping the clock.

K, I'm not sure what stepping the clock is, but... I'm I'll have to learn what to do to produce a cron job, if I find something, I'll post it here.  Or if anyone can help.

----------

## ichbinsisyphos

 *Dirk.R.Gently wrote:*   

> 
> 
> I'm kinda new to scripting so would putting something like this be safe?
> 
> ```
> ...

 

im totally unexperienced in bash myself, so i wont be of much help in the details. i was just trying to show you the right direction.

you need a logical connection between the commands.

in the shell,

```
/bin/ps aux | grep --invert-match grep | grep nm-applet && sleep 9 && /etc/init.d/ntp-client start
```

 will start ntp-clent after 9 seconds, IF nm-applet is running at the time of execution. if nm-applet is not running (if grep returns nothing), it will do nothing. so you should translate it to bash, put it in a loop and check maybe every second after X starts.

 *Dirk.R.Gently wrote:*   

> 
> 
> Is /bin/ps a list of processes that are running?  Hmm, even if network manager is running it wont mean its connected.  Maybe I can grep iwconfig somehow...
> 
> 

 

yes, sure. i thought networkmanager just starts, connects and done. then a simple "sleep" would do to give the programm time to make the connection.

im using a dsl-modem and ppp. i could for example do 

```
ifconfig | grep ppp0
```

grep anything that returns nothing when not connected, but does return something, when connected. the "| --invert-match grep" is not necessary usually, just when grepping ps aux, cause "grep something" is its own process.

 *Dirk.R.Gently wrote:*   

> 
> 
> Actually I think I found something easier:
> 
> When running ntpdate on a regular basis from cron as an alternative to running a daemon, doing so once every hour or two will result in precise enough timekeeping to avoid stepping the clock.

 

a cron script could be the easier way. but i had a lot of problems with it and found it unreliable. maybe just because i dont really understand it.

is your system clock really that far off, that you need to sync every hour and not just every time you boot?

anyway, maybe some more experienced user has more to say.

----------

## ichbinsisyphos

how about this?

```
while true

do

   if ifconfig | grep ppp0 >/dev/null

   sleep 5

   then

      /usr/sbin/ntpdate -s -b -u server

      echo 'ntp started'

      exit

   fi

done

```

edit: damn still the problem with permissions ...

edit edit: not really a problem, if you can put it in local.start, in the background ...

----------

## Dirk.R.Gently

I possibly have a solution.  NetworkManager runs as a daemon, and I just learned that there are ways I can pass calls to it.  Apparently nm-applet does it.  But I havent learned the calls yet.   In the /etc/conf.d/NetworkManager I learned you can have variables.  If I can have NetworkManager connect at boot, I'll eliminate my ntp-client startup dependency.  Documentation is however scarce, if I learn anything I'll post it here.

----------

## ichbinsisyphos

i dont know if and why you prefer a different solution, but this works here:

/etc/conf.d/local.start:

```
/home/sisyphos/ntp &
```

/home/sisyphos/ntp:

```
while true

do

        if ifconfig | grep ppp0 >/dev/null

        then

                /etc/init.d/ntp-client start

                break

        fi

sleep 5

done

exit

```

local.start, the last init script started, executes the ntp script in my home directory as root in the background, which checks every 5 seconds if the connection is online. if yes, it starts ntp-client and stops itself. works fine when i start my connection in X.

i guess you can find something to replace "ifconfig | grep ppp0"?

you need to make ntp executable with "chmod 755 /home/sisyphos/ntp". no guarantees for the effect of 755 though   :Very Happy: 

----------

## Dirk.R.Gently

 *ichbinsisyphos wrote:*   

> i dont know if and why you prefer a different solution, but this works here:
> 
> /etc/conf.d/local.start:
> 
> ```
> ...

 

I like the script it's functionally smarter than my solution.  I created a hack where I could launch the ntp-client init script as not-root and put that in the Sessions control panel.  I wonder about my script security-wise though.   Putting it in /etc/conf.d/local is a better idea.

I put the script in /usr/bin and named it ntp-client-init-nm-wireless.  Does this script have the ability to persistently check!?  It didn't seem to for me.  How does it loop?  Seems like it will need another function "else"?! 

```
#!/bin/bash

while true

do

        if ifconfig | grep ppp0 >/dev/null

        then

                /etc/init.d/ntp-client start

                break

        else

                sleep 300

                ntp-client-init-nm-wireless

        fi

done

exit

```

I didn't require the script to to very persistent 5 or so minutes would do.  Bah, this probably wouldn't do it as the script would call on itself over and over and have mulitple running instances?

----------

## ichbinsisyphos

does ifconfig | grep ppp0 return something for you?

the script checks every 5 seconds. after ppp0 is found it starts ntp-client and exits. supposedly, the break in the if-statement stops the while loop. at least here the script gives no more output, after ntp-client is started once.

do you need to sync more often than once at startup?

if you want to do that, i would put the existing while loop into another while loop with a longer sleep statement. the inner while loop always stops, after ntp-client is started.

i think in your example it checks for ppp0, wont find anything at the first try, sleeps 5 minutes and calls ntp-client-init-nm-wireless, which means it calls itself? it checks ppp0 again, will find something this time, starts ntp-client and stops. the while loop in the first instance starts again, greps ppp0, finds it and starts ntp-client, then stops.

whenever i try an else-statement here, it just gets stuck in the loop forever though, god knows why.

if you want to check its behaviour, put in a few echos

```

while true

do

        echo 'looking for network connection'

        if ifconfig | grep ppp0 >/dev/null

        then

                echo 'connection found'

                echo 'starting ntp-client'

                /etc/init.d/ntp-client start

                echo 'client started - exiting'

                break

        fi

        echo '!!!!!   no connection found   !!!!!'

sleep 5

done

exit 
```

on another thought, i makes no sense to start the client more than once. it will fail with "ntp-client has already been started" anyway.

does the client sync regularly? i didnt find any good documentation yet.

you can start 

```
ntpdate -s -u -b pool.ntp.org 
```

 more than once though.

----------

## Dirk.R.Gently

 *ichbinsisyphos wrote:*   

> i think in your example it checks for ppp0, wont find anything at the first try, sleeps 5 minutes and calls ntp-client-init-nm-wireless, which means it calls itself? it checks ppp0 again, will find something this time, starts ntp-client and stops. the while loop in the first instance starts again, greps ppp0, finds it and starts ntp-client, then stops

 

The script I created I gave up on because I misunderstood how scripts work.

 *Quote:*   

> does ifconfig | grep ppp0 return something for you?
> 
> the script checks every 5 seconds. after ppp0 is found it starts ntp-client and exits. supposedly, the break in the if-statement stops the while loop. at least here the script gives no more output, after ntp-client is started once.

 

Ah, with your new script, the echo functions helped.  The script wasn't syncing not because it wasn't attempting to loop - the script was exactly (almost) as it needed to be.  I assumed that I needed to create a loop in my script, but the truth is if the "if" statement isn't met then it automatically tries until if is met, and "else" isn't necessary.

 *Quote:*   

> do you need to sync more often than once at startup?

 

I'm using a laptop though I might do that when I put up a server. 

 *Quote:*   

> does the client sync regularly? i didnt find any good documentation

 

According to the Gentoo Wiki no.  I can't understand though having a daemon running though that can't.    :Confused: 

So I'm using your first script:

```
#!/bin/bash 

while true

do

        if ifconfig | grep ppp0 >/dev/null

        then

                /etc/init.d/ntp-client start

                break

        fi

sleep 300

done

exit
```

The only thing I did as up the time limit.  Running ntp-client while NetworkManager is initializing would return a "name resolution error"  So the only now that can go bad with this script is that by sheer coincidence ntp-client and NetworkManager try to run at the same time.    :Smile:    Appreciate the help!  Been thinking of putting this in the NetworkManager wiki on the Gentoo Linux Wiki if you don't mind.

----------

## ichbinsisyphos

 *Dirk.R.Gently wrote:*   

> Running ntp-client while NetworkManager is initializing would return a "name resolution error"  So the only now that can go bad with this script is that by sheer coincidence ntp-client and NetworkManager try to run at the same time.      Appreciate the help!  Been thinking of putting this in the NetworkManager wiki on the Gentoo Linux Wiki if you don't mind.

 

sure, i feel honored   :Razz: 

maybe there someone more experienced has some ideas. there has to be a more elegant and secure way to avoid collision with network-manager startup.

when do you get that error btw? i only get it, when i forget to close my internet connection before i (re)start my connection and local. because then ppp0 exists (and therefor the script finds it) from the old connection, while the new isnt ready yet. i never got that error at boot time.

----------

## hpastoriza

I just installed NetworkManager so I'm not sure if this works. But I believe that your answer is in:

man NetworkManagerDispatcher

----------

## Dirk.R.Gently

Tada!  This is my final script that works very well.

```
while true

do

        sleep 300

        echo 'verifying a network connection'

        if ifconfig | grep ath0 >/dev/null

        then

                echo 'connection found'

                echo 'starting ntp-client'

                /etc/init.d/ntp-client start

                echo 'client started - exiting'

                break

        fi

        echo '!!!!!   no connection found - retry 5 minutes  !!!!!'

sleep 300

done

exit
```

After reading the manual for NetworkManagerDispatcher this probably is a more proper spot for it: /etc/NetworkManager/dispatcher.d.  Unforutunately the man page doesn't say much about configurating the heretofore script.  I wonder if I have to enable the script to "/etc/init.d/ntp-client stop" as well...  I take the script and place it in the dispatcher.d folder and will see if it does what I hoped it would.  Thanks for the input.

----------

## Dirk.R.Gently

Getting the program the in dispatcher.d folder didn't' work.  And I'm unable to find any other documentation on it.  I put the information in the Wiki, thanks.

http://gentoo-wiki.com/NetworkManager

----------

## avieth

Thanks for adding that dispatcher information to the wiki, if it was you. Networkmanager has very poor documentation, not to mention the mystery that is knetworkmanager.

----------

