Skip to content
Advertisement

How to stop a shell script correctly?

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:

JavaScript

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:

JavaScript

Note: The script is always executed as sudo.

Does anyone know how to stop my shell script?

Advertisement

Answer

The “stop” check needs to come before you overwrite the pid file, and certainly doesn’t need to be inside the loop.

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement