4

I am using a GPS hat from adafruit.

According to the document

Start gpsd and direct it to use HW UART. Simply entering the following command:

sudo gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock

While this does in fact work, I am trying to find a way to automatically call this on a reboot. I've tried putting it in a .py file and calling it when the machine restarts in a cronjob but that doesn't work. (Invalid Syntax). Hoping I could be assisted in accomplishing this.

Thank you

Niana
  • 1,057
  • 2
  • 14
  • 42

1 Answers1

2

The fastest and easiest way is to put the above command in /etc/rc.local file (without sudo!). This is a shell script invoked on boot.

A more proper way of doing this is to create a service file into /etc/init.d directory. To start see any simple file into that directory, copy and modify it and make sure is executable. Basic (untested) example:

#!/bin/sh -e
### BEGIN INIT INFO
# Provides:          gpsd
# Required-Start:    
# Required-Stop:
# Default-Start:     1 2 3 4 5
# Default-Stop:
# Short-Description: Run my GPSd
### END INIT INFO
#

case "$1" in
  start)
    gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock
    ;;
  stop)
    killall -KILL gpsd
    ;;
  restart|force-reload)
    killall -KILL gpsd
    sleep 3
    gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock
    ;;
  *) echo "Usage: $0 {start|stop|restart|force-reload}" >&2; exit 1 ;;
esac

Once you have that make sure it is enabled on boot, so your system will automatically call service gpsd start. This is done with the update-rc.d command on Debian-base distros and systemctl on RHEL.

If you let us know your linux distro we can be more specific.

urban
  • 5,392
  • 3
  • 19
  • 45
  • it's a raspberry pi so, that's rasberrian jessie. – Niana Apr 10 '16 at 19:37
  • Not sure if related but the instructions on installing the GPS Hat asked for the killing of that gpsd serv.ce. quote here "Note if you're using the Raspbian Jessie or later release you'll need to disable a systemd service that gpsd installs. This service has systemd listen on a local socket and run gpsd when clients connect to it, however it will also interfere with other gpsd instances that are manually run (like in this guide). You will need to disable the gpsd systemd service by running the following commands:" – Niana Apr 10 '16 at 19:38
  • Seems then that you already have a `gpsd` file in `/etc/init.d` which is disabled and you just want to create a new one working differently? Maybe then `/etc/rc.local` is a better solution... not sure! – urban Apr 10 '16 at 19:42
  • I ran sudo nano /etc/rc.local and place the gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock at the end (after the part about the IP printing) yet it doesnt seem to run. Do I need to uncomment the first line? – Niana Apr 10 '16 at 19:45
  • Can you post your rc.local somewhere? Mine is empty but general rules are it starts with shebang (`#!/bin/sh -e`) and ends with `exit 0`. All your commands should be in the between (note that shebang line is a special comment and should remain a comment) – urban Apr 10 '16 at 19:47
  • 1
    `exit 0` exits the script with code 0 == success :) Nothing after that is being executed – urban Apr 10 '16 at 19:50
  • btw - accept if this answer helped you since I am fighting for the 1K rep :) – urban Apr 10 '16 at 19:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108762/discussion-between-niana-and-urban). – Niana Apr 10 '16 at 20:19
  • Note that recent GPS Hats now use /dev/serial0 not /dev/ttyAMA0 – Aneel Nov 05 '22 at 22:17