Bash script should only kill those instances of another script's that it has launched
Don't use the name to kill it. Since the calling.sh
script is calling the process you later want to kill, just use $!
(from man bash
):
!
Expands to the process ID of the job most recently placed into the background, whether executed as an asynchronous command or using thebg
builtin
So, if you're calling.sh
is like this:
called.sh &
## do stuff
pkill called.sh
Change it to this:
called.sh &
calledPid=$!
# do stuff
kill "$calledPid"
I've had to pick this but out of scripts so many times; it's even more fun when the scripts are called as part of a complex automated schedule. You really shouldn't rely on something like pkill
to select which script to kill.
Inside of calling.sh you should record the PIDs of the jobs you have started and kill them explicitly by PID.
Inside calling.sh:
./called.sh &
called_pid=$!
# Later
kill $called_pid