I've got a USB GPS receiver permanently attached to my raspberry pi. At boot time, I would like raspbian to wait until the GPS has a signal, then set the OS's time to whatever the GPS says.

I've already installed gpsd and gpspipe and I'm logging the output to a file.

Does anyone know how to achieve it?


The answer is to run a script which reads the GPS, then sets the system time. I found the following code here.

Create a bash file with the following:

date -s '01/01/2014 00:01'
sleep 1
pkill ntpd
pkill gpsd
gpsd -b -n -D 2 /dev/ttyUSB0
sleep 2
GPSDATE=`gpspipe -w | head -10 | grep TPV | sed -r 's/.*"time":"([^"]*)".*/\1/' | head -1`
date -s "$GPSDATE"

To convert from GMT to your local time zone, add the following:

FIDATE=`date --date='+3 hour'`
echo $FIDATE
date -s "$FIDATE"
sleep 1
| improve this answer | |
  • You might want to mark as answered then - how many minutes did it take you to find your own answer? ;-) – Wilf Nov 24 '13 at 11:52
  • Haha true. I researched for about an hour before asking, but I found it soon after asking. Ahh well – Fidel Nov 25 '13 at 1:22
  • I don't see an answer. Simply saying HERE and a link could be subject to link-rot. (This database will exist for years, remember. Would you be so kind as to show the answer, whatever it is? – SDsolar Jun 22 '17 at 7:26
  • You're right. Code added :) – Fidel Jun 22 '17 at 12:15
  • I didn't need any of the stuff around the GPSDATE= line, I just put the following in my crontab to pipe the current date from gps into date: gpspipe -w | head -10 | grep TPV | sed -r 's/.*"time":"([^"]*)".*/\1/' | head -1 | { read gmt; sudo date -s "$gmt"; } – GammaGames May 3 '19 at 22:24

I'm not sure if the script you have found is useful at all: parsing the output of gpspipe with a shell script is anything but fast. You'll most probably get worse clock accuracy using your script than you would by simply synchronizing with pool.ntp.org (unless you don't have network connection at all, or use a modem).

Both ntpd and chrony can use GPS time source directly, if you tell them so in the config. For example, check out chrony manual and search for refclock. You'll see the examples of configuration for different kinds of GPS devices (PPS or NMEA). Note that PPS (pulse per second) GPS devices require an additional clock source to be configured, to tell which second has just elapsed. For example:

server pool.ntp.org refid NTP
refclock PPS /dev/pps0 lock NTP

This will fetch your time from the network as usual, but will constantly readjust it to pulses form the PPS device.

For a NMEA GPS, the config would be:

 refclock SHM 1 poll 3 refid GPS1

Here is a nice tutorial which explains how GPS time servers work in greater detail.

Also check out this question.

| improve this answer | |

Don't forget that GPS time is not 100% equal to GMT. GMT is regularly adjusted with leap seconds so if you want your clock accurate to the second you will have to add some seconds to the GPS time. This is also explained in the answer to question 8552086

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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