I need to set date and time in my server in order to make them synced always. I cannot use ntp inside my channel due to proxy issues. I've found this workaround. It works fine when I run it in command line interface. However when I add that command into a crontab job it work unexpectedly ie. set's time 00:00:00 whenever it is run.

This is my command in crontab:

*/1 * * * * date -s "$(curl -sD - google.com | grep '^Date:' | tr -d '\r,' | awk '{print $2, $4, $3, $6, $7, $5}')"

I also tried to run as a shell script like:

*/1 * * * * /usr/local/sbin/test.sh

No luck! So how can I solve this problem? What is wrong with my crontab job?

  • You imply you're behind a proxy. How does curl know to use a proxy when it's run from cron? Have you considered what your cron command does when curl fails? – Kenster Sep 4 '14 at 12:08
  • 4
    No... Just no... – ewwhite Sep 4 '14 at 12:13
  • 7
    Don't do this. Work with the relevant people to get NTP working in your environment. You do not want to receive an abuse complaint from Google, and that's what lies at the end of this road. – Michael Hampton Sep 4 '14 at 12:19
  • 3
    I'm voting to close this as a duplicate of the canonical crontab question, but only because there's no option for "this is the insanity to top all insanities" – Jenny D Sep 4 '14 at 12:24
  • @MichaelHampton Thanks for your suggestion you are correct. It was just a short term solution but you are right using an local ntp server would be a way better solution. – huzeyfe Sep 4 '14 at 13:52

The environment may not be the same in the crontab job than when you run it in a shell. Try to follow https://stackoverflow.com/questions/2135478/how-to-simulate-the-environment-cron-executes-a-script-with to runs with the same environment, then may be you'll find out the issue.

Also I hope you won't change the date every minute in your final script :-)

  • 1
    Thank you very much for you suggestion but I've changed my mind to use local ntp server. – huzeyfe Sep 4 '14 at 13:53

You don't need to use every component of the date, only the hour, minute and second will suffice:

*/1 * * * * date -s `curl -sD - google.com | grep '^Date:' | tr -d '\r,' | awk '{print $6}'` > /dev/null

I don't think your server time will drift enough to slip to another date, but only a few seconds.

The > /dev/null is useful to avoid receiving one cron email every minute.

  • Thank you very much for your suggestion especially > dev null as it s a perfectly valid reason but I've changed my mind to use local ntp server. – huzeyfe Sep 4 '14 at 13:54

I can suggest you to use htpdate.

It works behind the proxy server and can be useful to enable time synchronization in local networks. You can use one of your corporate servers accessible from intranet but having the correct time.

For example, query serverfault.com behind the proxy with IP and port 8080.

htpdate -d -q serverfault.com -P

Or query multiple hosts directly:

htpdate -d -q google.com www.nist.gov

You can run it as a daemon passing -D option.

This utility has two cons: it doesn't perform small time adjustments like normal ntp does, also it will not guarantee the correct time stratum because it totally depends on other webservers time and might not correctly compensate network roundtrip timings.

Anyway, this utility is easier to work with, supports some redundancy options and it is my top choice in environments where regular ntp sync cannot be used.

  • 3
    Why? What advantages does it offer? What disadvantages does it come with? You're making a valid suggestion, but answers should be more than a suggestion and an offsite link. – 89c3b1b8-b1ae-11e6-b842-48d705 Sep 4 '14 at 13:32
  • @zoonman thank you very much for this alternative. I've changed my mind to use local ntp server however I would like to hear alternatives and benefits. – huzeyfe Sep 4 '14 at 13:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.