Find the answer to your Linux question:
Results 1 to 7 of 7
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Automatic time setting in CentOS


    Hello everyone,

    I'm in need of a script/command that will get the current time from a time server , add 10 minutes , and then set the adjusted time as the system time.
    ( I need the system time to be +10 minutes from the correct time )

    I thought about using the ntpdate, to set the correct time,
    then use some command to advance the time by 10 minutes,
    and put it in a shell script that will be called periodically by cron.

    Any help will be greatly appreciated,

  2. #2
    Quote Originally Posted by staso% View Post
    Hello everyone,

    I'm in need of a script/command that will get the current time from a time server , add 10 minutes , and then set the adjusted time as the system time.
    ( I need the system time to be +10 minutes from the correct time )

    I thought about using the ntpdate, to set the correct time,
    then use some command to advance the time by 10 minutes,
    and put it in a shell script that will be called periodically by cron.

    Any help will be greatly appreciated,
    you pretty much said all you need to do. what have you tried so far?

    what is the time server, exactly? is it an NTP time server, or a xinetd-based time protocol server? or just a server that you ssh to and grab the time from?

  3. #3
    Quote Originally Posted by atreyu View Post
    you pretty much said all you need to do. what have you tried so far?

    what is the time server, exactly? is it an NTP time server, or a xinetd-based time protocol server? or just a server that you ssh to and grab the time from?
    The time server is a NTP server.
    I've successfully updated the time using ntpdate,
    but I can't seem to find the correct command to advance the time.
    I've looked into 'time' and 'date'... with no results.

    Also it would be better to get the correct time as a variable of some sort,add the 10 minutes and only then set it.
    But I didn't come up with a way to just query the time with ntpdate.

    Thanks a lot for helping,

  4. $spacer_open
    $spacer_close
  5. #4
    Quote Originally Posted by staso% View Post
    The time server is a NTP server.
    I've successfully updated the time using ntpdate,
    but I can't seem to find the correct command to advance the time.
    I've looked into 'time' and 'date'... with no results.
    I know the date command does it for you, if you want to add Years, Months, or Days - I didn't see an example for adding seconds though.
    Also it would be better to get the correct time as a variable of some sort,add the 10 minutes and only then set it.
    But I didn't come up with a way to just query the time with ntpdate.
    i do a similar thing myself (get date from an ntpdate server then do something w/that). what i had to do is grep the output of the command for the time. here's an example:

    Code:
    ntpdate -d 0.pool.ntp.org|grep originate\ timestamp
    Here's a tip: using the date command, you can convert the date from the above command (you might need to format it a little first) to a number of seconds (seconds since the Linux epoch) using the %s format parameter. Than add 10 seconds(or whatever) to the number of seconds. Then you have to use funky syntax to convert back from epochal linux seconds to a standard date/time string. See the bottom of this wiki for an example:

    http://en.wikipedia.org/wiki/Date_%28Unix%29

  6. #5
    Thanks atreyu,

    I've decided to go with the simpler solution :

    #!/bin/sh
    ntpdate 0.pool.ntp.org
    date -s "10 minutes"

    Thanks for the tip about the 'date' command being able to add time, that's exactly what I've wanted !

  7. #6
    Quote Originally Posted by staso% View Post
    date -s "10 minutes"
    aha! i *knew* there was a way to do it w/date...just didn't know how...

  8. #7
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,950
    Tbh, I dont like the above solution.
    - you cannot run ntpd, which is the standard way of synchronizing time.
    - a cron is used instead. But what does it do? It sets the correct time, then +10min.

    So the server is jumping in time regularly.
    This can confuse daemons.
    And I am not only talking about timestamps for logfiles, but also e.g. timer, benchmarks,..

    My suggestion would be to sync the time with ntpd,
    but define a custom timezone, which is 10min ahead.

    So the procedure would be to copy your regular zoneinfo file, then dump it, modify it, create a new binary file with the zic compiler and lastly of course set this custom file as /etc/localtime

    These links might be a start:
    tz database - Wikipedia, the free encyclopedia
    TimeZoneChanges - Debian Wiki

    Be aware, that certain tools like java maintain their own zoneinfo files.
    You must always face the curtain with a bow.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •