title: How to stop a shell script correctly?
body: <p>I've written a small bash script to start a program every 3 seconds. This script is executed on startup and it saves its PID into a pidfile:</p>
  
  <pre><code>#!/bin/bash
  
  echo $$ &gt; /var/run/start_gps-read.pid
  
  while [ true ] ; do
      if [ "$1" == "stop" ] ;
      then
          echo "Stopping GPS read script ..."
          sudo pkill -F /var/run/start_gps-read.pid
          exit
      fi
      sudo /home/dh/gps_read.exe /dev/ttyACM0 /home/dh/gps_files/gpsMaus_1.xml
      sleep 3
  done
  </code></pre>
  
  <p>The problem is, I can't terminate the shell script by calling <code>start_gps-read.sh stop</code>. There it should read the pidfile and stop the inital process (from startup).</p>
  
  <p>But when I call <code>stop</code>, the script still runs:</p>
  
  <pre><code>dh@Raspi_DataHarvest:~$ sudo /etc/init.d/start_gps-read.sh stop
  Stopping GPS read script ...
  
  dh@Raspi_DataHarvest:~$ ps aux | grep start
  root       488  0.0  0.3   5080  2892 ?        Ss   13:30   0:00 /bin/bash /etc/init.d/start_gps-read.sh start
  dh        1125  0.0  0.2   4296  2016 pts/0    S+   13:34   0:00 grep start
  </code></pre>
  
  <p>Note: The script is always executed as <code>sudo</code>.</p>
  
  <p>Does anyone know how to stop my shell script?</p>