# Time updating Job ?



## AJ-BSD (Oct 3, 2012)

Hello everyone!
Still new to FreeBSD (and BSD), and liking it more and more.

Would like your feedback and ideas on a small issue.
My FreeBSD machines are running as VMs (Virtual Machines).

When I stop using them, I "halt" them.

Yet when I power them back up, their clock is off-time by x-amount.

I was wondering what would be the best way to get it updated?
I thought of creating a small script that does something like:

```
ntpdate time.nist.gov
```

Yet then I thought maybe it would be easier/more convenient to do it on a cronjob that runs every 10 minutes.

What are your thoughts?
Would you do it another way?
If there a "sub-system" or "job" of BSD that solves this special situation I have running?

Thanks a ton.
I know that it can be done... I guess I'm looking for "best practice" or how would more knowledgeable people do it.

Have an awesome day!


----------



## kpa (Oct 3, 2012)

The standard solution is to use ntpd(8).

In rc.conf:


```
ntpd_enable="YES"
ntpd_sync_on_start="YES"
```

Edit /etc/ntp.conf:


```
server 0.XX.pool.ntp.org iburst maxpoll 9
server 1.XX.pool.ntp.org iburst maxpoll 9
server 2.XX.pool.ntp.org iburst maxpoll 9
server 3.XX.pool.ntp.org iburst maxpoll 9
```

Where XX is the two-letter code of your country.


----------



## AJ-BSD (Oct 3, 2012)

Thanks!
I'm still a n00b, so I'm probably mis-understanding what's going on.
Yet, doesn't rc.conf run during start-up?
Would I have to start-up the machine in order to have it update via ntpd?

I can do that, it's just that I "halt" the virtual machines whenever I stop using them.
Just wondering.

Thank you!


----------



## SirDice (Oct 3, 2012)

If your hosting provider has an NTP server it's best to use that one.


----------



## kpa (Oct 3, 2012)

You can start the service manually without a reboot like this:

`# service ntpd start`

With those settings in /etc/rc.conf the ntpd(8) service is started automatically on boot, the second setting guarantees that the clock gets synced even if it's off by a large amount.


----------



## SirDice (Oct 3, 2012)

AJ-BSD said:
			
		

> Yet, doesn't rc.conf run during start-up?


Yes.



> Would I have to start-up the machine in order to have it update via ntpd?


How are you supposed to run anything if the machine is turned off?


----------



## AJ-BSD (Oct 3, 2012)

SirDice said:
			
		

> How are you supposed to run anything if the machine is turned off?



Oh, clearing up on my part.

The thing is:
Say I start-up the machine from zero (virtual machine).
I do my stuff... all good.
When I'm going to finish using it, I "halt" the virtual machine.

So I didn't turn her off... I just left her... I dunno if "digital limbo" would be the best way to describe it.

Later, when I'm going to user her again, I start-up the virtual machine.
Yet she doesn't go through the boot-up process.
She starts-up where I left off.

So that's why I ask about the rc.conf at start-up.
And why I thought to have a cronjob (crontab?) to run every 10 minutes.


----------



## chatwizrd (Oct 3, 2012)

I just run this cron:


```
#Sync network time
*/15 * * * * /usr/sbin/ntpdate -4 -u us.pool.ntp.org >/dev/null 2>&1
```


----------



## AJ-BSD (Oct 5, 2012)

chatwizrd said:
			
		

> I just run this cron:
> 
> 
> ```
> ...



Thanks for this!

I'm still new to this, so would like help on understanding what is going on here.

The "*/15 * * * *" I understand.
That's the "when" to run it (time schedule?).

The "/usr/bin/ntpdate -4 -u us.pool.ntp.org" I also understand.
It's the ntpdate command with two flags (4 and u).
I will read the man pages now to see what these flags to.
The us.pool.ntp.org is the server where we're taking the time from.
((I'll check with my ISP to see if they have a time server))


What I don't understand:
Why the ">/dev/null"?
I do know that /dev/null is "nothing-ness"... so stuff gets sent there to die a digital death.
Yet... why?
The best I can come up with: you're sending any returned messages (error?) to /dev/null.
If that's the case... what would happen if that line wasn't present?

Is there a "standard log file" where these messages go?

And finally...
What is the "2>&1" for?
That... I have no idea.

THANK YOU everyone!
Very happy over here!


----------



## SirDice (Oct 5, 2012)

AJ-BSD said:
			
		

> The best I can come up with: you're sending any returned messages (error?) to /dev/null.
> If that's the case... what would happen if that line wasn't present?
> 
> Is there a "standard log file" where these messages go?


Any output generated by a cronjob will get emailed to the user that runs the cronjob. The redirection to /dev/null simply prevents this as all output is send to the bit bucket.



> What is the "2>&1" for?



> /dev/null redirects STDOUT to /dev/null. 2>&1 redirects STDERR to STDOUT.

http://tomecat.com/jeffy/tttt/shredir.html


----------



## AJ-BSD (Oct 5, 2012)

Awesome!
Good stuff!
Thank you, and to everyone =D


----------



## AJ-BSD (Oct 9, 2012)

Hello guys,
I tried this exact line:

```
#Sync network time
*/15 * * * * /usr/sbin/ntpdate -4 -u us.pool.ntp.org >/dev/null 2>&1
```
Inside /etc/crontab

Yet it doesn't automatically update.
I did the modification logged in as root.
Any ideas on what could be going on?

I thought that maybe the server wasn't resolving... yet if I run the command manually in the prompt, the time automatically adjusts.

I run:
`# /usr/sbin/ntpdate -4 -u us.pool.ntp.org`
And the time adjusts immediately.

Not sure what's going on.
Thanks!


----------



## SirDice (Oct 9, 2012)

Have a look in /var/log/cron and see if it's started correctly.


----------



## AJ-BSD (Nov 8, 2012)

I wanted to update:
I found the problem!

When I check the logs as SirDice said, nothing was coming up.

So then I kept checking, until on a hunch:
The crontab was setup for "user123", yet I had modified/deleted it (or something) with the user root.

So apparently, the permissions were not set right.
I did chmod 755, and all is good now.

She does a time-sync every 15 minutes.

Thanks to everyone... your help is very much appreciated.
The community in these forums is awesome!


----------

