Trying to set date on a Linux based machine from another machine

12,360

Solution 1

Using a cron job, you could write a file to your /tftp directory with a granularity of 1 minute.

* * * * * date "+%m%d%H%M%Y.%S" > /tftp/currdate.txt 2>/dev/null

The contents of that file are a single value, which is formatted conveniently in the same format the date command needs to set the date/time.

bash$ cat currdate.txt
102600052011.00

On the busybox side of things, you could just tftpget the file, and process it

cat currdate.txt | while read date; do
    date $date
done

If HTTP is an option, you could set up a php script that just returned the date when called, and have your script on the busybox side poll that URL, and process the result. The granularity of the date would be closer, in that case.

With TFTP, you'll be within 1 minute. Hopefully that's close enough.

Solution 2

NTP (Network Time Protocol) is the standard way of getting the date. On a BusyBox system, you might want to use ntpclient.

You'll need an NTP server somewhere on the network.

Solution 3

To use the Network Time Protocol on the embedded machine, just run some lightweight NTP daemon (server) locally on your LAN - for example on the machine that you are currently trying to use for synchronization. Most, if not all distributions have packages like openntpd or ntp that provide an NTP server.

If you still insist on parsing the output of date, the best way is to use the generic +%s (seconds since 1970) format for both output and input. To do so, on the date source side use date +%s to obtain the date string and on the busybox machine, run date -s "@$VALUE", where $VALUE should be replaced with the value received from the source. Note that this approach must end up in results skewed because of communication and command execution times (which are both minimized when you make use of NTP).

Solution 4

You can also use:

# ntpd -q -p ntp-server-ip-address

ntpd will update the time and quit afterwords. You can have your firmware execute this on every boot for example.

Solution 5

As per your comment.

Thanks for the answers. rdate is not available on my platform. Also I cant use ntp, since I cant access internet on the board. I am more interested in a way to be able to parse the output of date from another machine and set that here at the moment

The solution to your problem is to setup another machine on your LAN as a time server -- that machine should (preferably but not strictly necessarily) have access to internet, so that it itself can synchronize with an external time source. the NTP server can configured to broadcast the clock on the LAN (1 UDP packet every 5 minutes or so), or it can just be a server which is accessed on request -- your choice.

The your busy-box which has LAN access (but not internet access) can be installed with a NTP client which picks up the time from the broadcast OR just asks your LAN server for the time when needed.

Share:
12,360

Related videos on Youtube

bobby
Author by

bobby

Updated on September 18, 2022

Comments

  • bobby
    bobby almost 2 years

    I am running busy box on an embedded platform. I would like to set the date on the box properly each time I reboot the hardware. I have no way to save the time persistently, so am reduced to setting the time each time. I have LAN connectivity for it, but no SSH. My current hack for this is to redirect the output of 'date' to a file on my PC, access that file from my hardware platform through tftp, and then somehow parse the file, use its contents and then to set the date. I have no idea how to get the string from the file and then set it using the 'date' command. Any help would be appreciated. Any other way to set the date would be get. I have tried using ntpd, but that fails due to lack of internet connectivity

    • Paul Tomblin
      Paul Tomblin over 12 years
      In the old days, we used rdate -s. That was before ntp came along.
    • rozcietrzewiacz
      rozcietrzewiacz over 12 years
      @PaulTomblin I still use rdate for the simple clock corrections that I need on my desktops.
    • bobby
      bobby over 12 years
      Thanks for the answers. rdate is not available on my platform. Also I cant use ntp, since I cant access internet on the board. I am more interested in a way to be able to parse the output of date from another machine and set that here at the moment
    • Paul Tomblin
      Paul Tomblin over 12 years
      If you have a network connection to "another machine", you can run the ntp server there, and the ntp client on your "board".
  • bobby
    bobby over 12 years
    Thanks for the hint on the date parsing.I will be using that as a fallback. I am aware that skewed times will be obtained, I am using this option as "Something better than nothing" ;-) I will be trying to get a ntp server working on my LAN.
  • bobby
    bobby over 12 years
    This is in short what I was looking for. I know that running a NTP server is the best solution, but in my current scenario, this will do. I didnt know about cron jobs and the script to parse the date helped. It works now. One change I did, was I wrote a script to update the date, and scheduled that script in the cronjob. The way you wrote, didnt work for me
  • Ghanima
    Ghanima almost 10 years
    The OP states to have no SSH connectivity on the machine.