Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

Set server time


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
St_Aug_Beach_Bum
Asked:
St_Aug_Beach_Bum
  • 10
  • 4
  • 3
  • +1
1 Solution
 
ZylochCommented:
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
 
TaintedGodCommented:
It might help if you tell us which operating system you are on.....Unix?
0
 
St_Aug_Beach_BumAuthor Commented:

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ZylochCommented:
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
 
St_Aug_Beach_BumAuthor Commented:

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
 
St_Aug_Beach_BumAuthor Commented:

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
 
St_Aug_Beach_BumAuthor Commented:
and it set the time.

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

agghh!  and my daughter is eating catfood while I do this!  Gotta go,  I'll check back a little later....
0
 
ZylochCommented:
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
 
Marcus BointonCommented:
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
 
St_Aug_Beach_BumAuthor Commented:

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
 
Marcus BointonCommented:
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
 
ZylochCommented:
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
 
St_Aug_Beach_BumAuthor Commented:

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
 
St_Aug_Beach_BumAuthor Commented:

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

locate ntpd

locates nothing either...
0
 
Marcus BointonCommented:
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
 
St_Aug_Beach_BumAuthor Commented:

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now