Subscribe to Grim's Projects        RSS Feed

Raspberry Pi's Internal Clock

Icon Leave Comment
The RasPi has no internal clock. Whenever it boots its internal clock is set to the Unix epoch. While this may not be a problem, I find it somewhat disconcerting. It did however cause an error in one particular application that I was trying to run that made use of system time.

The model B is the RasPi that has the ethernet port, and with the standard Arch distro it will attempt to update its internal clock from the web. Assuming you're always connected to the web (via ethernet or wireless adapter or some other method) then you can update your time this way.

What happens though if you disconnect from the web (or have a model A which has no ethernet port)? Well again, when you boot the time will reset to epoch, which I didn't much care for. I wanted a way for the RasPi to reinitialize from its last known good/updated/actualized time. I did this as follows:

First, I connected to the web and let the system clock set itself from the web. Now I had an actualized time I could make use of. Second I created a folder in my home directory called "systemd"
#$ mkdir systemd
Then I created a file that would contain a timestamp of whatever the current system time happened to be (when the following command is executed) as follows:
#$ date -R > systemd/time.stamp
I then proceeded to create two bash shell scripts, raspi-store and raspi-restore within the previously created systemd folder, by using nano. Their contents, respectively, are as follows:
date -R > /home/tron/systemd/time.stamp
date -s "`cat /home/tron/systemd/time.stamp`"
raspi-store saves the current system time to a file named time.stamp. Conversely, raspi-restore reads the saved system time from that file and sets the system time to its contents.

These two shell scripts are necessary in order to create two systemd services. The first service will use raspi-restore to set the system time BEFORE the network starts up. This will allow the time to still auto-correct itself if you're connected to the web. The second service will log the current system time when you shutdown your system. Thus enabling you to restore it on boot whenever the system is powered on again.

To create these services, you will need to create two files (as the root user) within the /usr/lib/systemd/system folder named raspi-store.service and raspi-restore.service. Their contents, respectively, are as follows:
Description=Store RasPi time


Description=Restore RasPi time


Once these files are created you will then issue the following commands (as root):
#$ systemctl daemon-reload
#$ systemctl enable raspi-store
#$ systemctl enable raspi-restore
Which will install them to your system so that they will be utilized on boot. Bear in mind that the filesystem locations preceded by /home/tron/systemd are the locations on my own RasPi. You will need to alter these to match your particular setup.

Also don't forget to chmod raspi-store and raspi-restore shell scripts so that they will have proper executable permissions, as follows:
#$ chmod 0755 /home/tron/systemd/raspi-store
#$ chmod 0755 /home/tron/systemd/raspi-restore

0 Comments On This Entry