Solved

Set server time

Posted on 2004-08-05
18
258 Views
Last Modified: 2010-08-05

Is there a way to do this with a php script, or do I need to use another language?  Currently I telnet in and do it by hand.
---------------
//get exact time

rdate tock.usno.navy.mil

//set it on server (I just type the time in by hand right now)

date --set="Thu Jan 15 15:10:00 2004"  

//set it into the hardware

hwclock --systohc
-------------

If I can get a php script for this, I can run it by cron once a week.

Thanks,    Chris
0
Comment
Question by:St_Aug_Beach_Bum
  • 10
  • 4
  • 3
  • +1
18 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 11729364
Hi

If your server allows it, it might be possible to do with system(). Try this:

$result = system('date --set="Thu Jan 15 15:10:00 2004"');
echo($result);

to see if it works.

Regards,
Zyloch
0
 
LVL 2

Expert Comment

by:TaintedGod
ID: 11729625
It might help if you tell us which operating system you are on.....Unix?
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 11729744

opps, sorry 'bout that, guess some system info would help.  It's a linux, ensim.

Zyloch, I just set up a file with your script, via telnet ran 'php testtime.php', and the entire script was echo'd back:

$result = system('date --set="Thu Jan 15 15:10:00 2004"');
echo($result);

but the time was not set.
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 11729814
Hmm... Try this, then and see what you get:

$result = system("date --set=\"Thu Jan 15 15:10:00 2004\"", $otherresult);
echo($result+"\nFDSHOIFHDSOIFHSDHFDSOIHDFIODSFH\n");
echo($otherresult);
//The weird text in middle is just a separator so we can see things more clearly.

Regards,
${Zyloch}
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 11730323

that seems outputting be outputing the actual script without parsing it.

Via telnet, I run:

php timetest.php

and get back:

Content-type: text/html
X-Powered-By: PHP/4.3.3

$result = system("date --set=\"Thu Jan 15 15:10:00 2004\"", $otherresult);
echo($result+"\nFDSHOIFHDSOIFHSDHFDSOIHDFIODSFH\n");
echo($otherresult);
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 11730395

oh, um, mark one up for stupidity!

Forgot the <?  ?>

Now I get:

Content-type: text/html
X-Powered-By: PHP/4.3.3

Thu Jan 15 15:10:00 EST 2004
00
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 11730421
and it set the time.

Now I need to fetch the correct time from navy.mil
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 11730450

agghh!  and my daughter is eating catfood while I do this!  Gotta go,  I'll check back a little later....
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 11730989
Sorry for the late reply. Optimum Online is giving me hell right now. Anyways, you should be able to get the time like this:

<?php
$getTime = system("rdate tock.usno.navy.mil", $moreInfo);
echo($getTime + "\nHFHFHFHFHFHFHFHFH\n");
echo($moreInfo);
?>

You can see which one you need.


Regards
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 25

Expert Comment

by:Squinky
ID: 11731499
Er, why don't you just let ntpd do its thing? You'll get way better accuracy and reliability than PHP and cron can deliver. I have 2 servers 5,000 miles apart and they're never more than a few milliseconds adrift. Anything over a few seconds is a good recipe for data loss, plus if you have to remember to set it, you won't.
You don't say what flavour of linux you're running, but it might be as simple as running redhat-config-time and you're 1 checkbox away from solid sync.
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 11732482

Zyloch, I get:
--------------------
[root@jessica root]# php timetest.php
Content-type: text/html
X-Powered-By: PHP/4.3.3

rdate: [tock.usno.navy.mil]     Thu Aug  5 20:56:44 2004

00

[root@jessica root]#
--------------------

So an empty result for $getTime and '00' for $moreInfo, but the result does show up inline.

====

Squinky,

ntpd... I don't know much about that, but I don't think it's on my server.  I asked in another forum, other Ensim users, and no one was able to give me any instructions about it.

I have linux 7.3 with Ensim.
0
 
LVL 25

Expert Comment

by:Squinky
ID: 11733926
You can be pretty sure ntpd is already on your server, and it's probably already running, just not talking to external sync sources.

On a command line, enter:

ps -aux | grep ntpd

You should see a line like:

root     123   0.0  0.0    18320    344  ??  Ss    9:09AM   0:00.03 ntpd -f /var/run/ntp.drift -p /var/run/ntpd.pid

If it's really not there, install this RPM; http://fr.rpmfind.net//linux/RPM/fedora/2/i386/ntp-4.2.0-7.i386.html

Redhat 7.3 has a program called dateconfig that does the same as redhat-config-time in later versions. You can run that and it will configure ntpd for you.

This thread may be of help too:
http://www.linuxquestions.org/questions/showthread.php?threadid=57167

There are very good reasons for using ntpd rather than a brute-force hard-sync approach, not least that it's the 'right' way to do it. ntpd doesn't do large step-changes to your clock, but smoothly advances or retards it to stay in sync (known as drifting). Otherwise you could have a situation where your clock jumps backwards in time, leaving you with files that appear to have been modified some time in the future, which could be enough to screw up an incremental backup. You can force a hard sync by running ntpdate, and you can see the current state of your sync by running ntpq -p.
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 11735537
You should be able to use preg_match to get the date from $result. I'll show you how after I get back home.
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 11736075

Squinky, you argument for using ntpd is persuasive :)

When I ran:

ps -aux | grep ntpd

I got:

root     17987  0.0  0.1  1740  552 pts/0    R    09:33   0:00 grep ntpd

But upon running:

dateconfig

I get:

bash: dateconfig: command not found


0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 11736169

I don't seem to have ntpd in /etc/rc.d/init.d, should I?
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 11736212

locate ntpd

locates nothing either...
0
 
LVL 25

Accepted Solution

by:
Squinky earned 500 total points
ID: 11737176
ok that search just found your command that was displaying the results for your search! Looks like you don't have it - go grab that RPM linked above. You install it by doing rpm -i filename.rpm

Dateconfig is also available as an RPM; http://rpmfind.net//linux/RPM/redhat/updates/7.3/i386/dateconfig-0.7.5-7.i386.html
However, unless you have X11 working, I suspect you won't be able to use it, so you'll need to config ntp the "hard" way, which that thread I mentioned should help with, and this guide looks good: http://freeunix.dyndns.org:8088/site2/howto/NTP0.shtml

ntp has a dual purpose. It can set your clock by syncing with an external server, but it can also be run as a source that other systems running ntp can set their clock by - it's common for a cluster to have 1 machine getting external time and all others syncing to that.

BTW I like your comments about your little one - I have a 9 month old girl that's such fun. Totally out of sync though ;^)
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 11737416

Squinky: Yep, mine is 17 months old now, born 2 months premature, so she is small for her age, but going strong, walking all over the place now, and willing to eat everything (loves the chinese buffet restaurant, and cat food of course).  Much more fun that I ever expected :)  

I found another forum with line by line instructions for installing ntpd.  Between that and the links you provided I should be able to get this set up.  If not, I willl come back with more points and questions!

Zyloch: thank you for your proposal as well, I will give Squinky's solution a go though.

You'all are great, thanks again,   Chris
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now