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:
#!/bin/bash
echo $$ > /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
The problem is, I can't terminate the shell script by calling start_gps-read.sh stop. There it should read the pidfile and stop the inital process (from startup).
But when I call stop, the script still runs:
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
Note: The script is always executed as sudo.
Does anyone know how to stop my shell script?
The "stop" check needs to come before you overwrite the pid file, and certainly doesn't need to be inside the loop.
if [ "$1" = stop ]; then
    echo "Stopping ..."
    sudo pkill -F /var/run/start_gps-read.pid
    exit
fi
echo "$$" > /var/run/start_gps-read.pid
while true; do
    sudo /home/dh/gps_read.exe ...
    sleep 3
done
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With