Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
1  
In the old days, we used rdate -s. That was before ntp came along. –  Paul Tomblin Oct 25 '11 at 12:57
    
@PaulTomblin I still use rdate for the simple clock corrections that I need on my desktops. –  rozcietrzewiacz Oct 25 '11 at 13:04
    
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 –  jogabonito Oct 25 '11 at 13:17
3  
If you have a network connection to "another machine", you can run the ntp server there, and the ntp client on your "board". –  Paul Tomblin Oct 25 '11 at 13:23
    
When I execute ntpd--help, the output I get is the same as at [link]lists.busybox.net/pipermail/busybox/2010-August/073087.html. (Link used for readability) So I guess I will have to used ntpd -p IP –  jogabonito Oct 25 '11 at 16:16

5 Answers 5

up vote 4 down vote accepted

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.

share|improve this answer
    
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 –  jogabonito Oct 28 '11 at 9:14

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.

share|improve this answer

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).

share|improve this answer
    
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. –  jogabonito Oct 25 '11 at 16:00

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|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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