Skip to content

Instantly share code, notes, and snippets.

@dustinlbarnett
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustinlbarnett/1a96793b6d232847886f to your computer and use it in GitHub Desktop.
Save dustinlbarnett/1a96793b6d232847886f to your computer and use it in GitHub Desktop.
Set system clock using GPS hardware
#!/bin/bash
# This script attempts to read the current time from the gpsd daemon by using the gpspipe command to grab gpsdsentences.
# Before attempting to get time, it checks if there is a gps fix to ensure invalid time isn't sent to the system clock.
# gpspipe output is formatted and sent to the date program to update the date/time.
#
# Dependencies:
# gpsd
# gpsd-clients
ATTEMPT=0
GPSFIX=0
TIMESET=0
MODE=0
while [ $TIMESET -lt 1 ]
do
while [ $ATTEMPT -lt 12 ]
do
if [ $GPSFIX -lt 1 ]
then
while [ $MODE -lt 2 ]
do
MODE=`gpspipe -w | head -10 | grep TPV | sed -r 's/.*"mode":([0-9]).*/\1/' | head -1`
GPSFIX=$MODE
let "ATTEMPT=ATTEMPT+1"
TIMESET=10
sleep 5
done
else
echo "GPS fixed, getting time..."
GPSDATE=`gpspipe -w | head -10 | grep TPV | sed -r 's/.*"time":"([^"]*)".*/\1/' | head -1`
echo "date -s $GPSDATE"
date -s $GPSDATE
TIMESET=1
ATTEMPT=100
fi
done
done
if [ $ATTEMPT = 100 ]
then
echo "Success"
else
echo "Unable to get time from GPS device."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment