1

I am trying to set the system clock at a specific time each day (1:49 AM local time) so that I can send a command to close a relay at exactly 2:00 AM local time to reset the clock on a external device that can't access NTP servers. When I try to edit the crontab file, it will not let me save changes. I believe the code that I want to add is:

49 1 * * * ntpd -s 3.us.pool.ntp.org 2.us.pool.ntp.org

Can someone verify this for me? How do I edit the crontab file and save changes?

2
  • 1
    crontab -e will open vi. To start editing type i. To save in vi press escape, type :wq, and press enter.
    – Gerben
    Dec 18, 2013 at 19:33
  • If you only want to sync the clock occasionally ntpdate might be more what you are looking for.
    – hildred
    Dec 19, 2013 at 3:25

2 Answers 2

3

Just login as root and run crontab -e. A editor will appear, letting you add the cronjob.

The syntax for the cronjob is correct, running at 1:49 AM.

Crontab syntax overview (source):

# * * * * *  command to execute
# ┬ ┬ ┬ ┬ ┬
# │ │ │ │ │
# │ │ │ │ │
# │ │ │ │ └───── day of week (0 - 7) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
# │ │ │ └────────── month (1 - 12)
# │ │ └─────────────── day of month (1 - 31)
# │ └──────────────────── hour (0 - 23)
# └───────────────────────── min (0 - 59)
3
  • Why run as root? Does ntpd require root access?
    – Gerben
    Dec 18, 2013 at 19:35
  • Actually I have no idea if ntpd requires root. I would just assume so, since I would not let non-root users adjust my clock. Alteast in a server environment nonroot users should absolutely not be able to tamper with the system clock. But please look it up, I am not sure. :-)
    – Ragnar123
    Dec 18, 2013 at 22:26
  • 1
    @Gerben ntpd only requires root access to set the date, and to get a low port number.
    – hildred
    Dec 19, 2013 at 3:24
1

why do you need to set the system clock exactly at 2AM ? install "ntpdate" package and forget about clock syncing, it will be done totally automatically.

the problem you need to solve is to send a command to your devices to set their clocks, and that command would be, as you wrote, something along the lines of:

49 1 * * * /your/command/to/control_the_remote_device

you may enter this command in "crontab -e", which will install this as a cron job for your user (no need for root). if you don't like "vi", use "export EDITOR=nano" to set your favourite editor to be used when editing the cron file.

ps. just don't touch ntp, it works perfectly as it is =)

1
  • Thanks everyone for the info. I need to send a contact closure to school consoles that have an internal clock that is drifting a few seconds a day. In the days before cell phones, etc. no one cared if the systems were off by even a few minutes. Now, everyone at the schools has a fit that the school clocks are not EXACTLY on. The school consoles are older technology that can only have the clock reset by a contact closure at exactly 2AM local time. So I first need to make sure the RaspberryPi is exactly on time to the second each day and then send a relay command to close at exactly 2AM. Thanks!
    – John
    Feb 10, 2014 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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