Hi. I've been doing bash programming for quite a while but I really can't figure out what's the problem here. This is what I'm trying to do:
I have multiple instances of a bash script running, let's say the name of the script is "myparent". Each instance launches external commands like tail or cat, and both the myparent processes and the child processes are continuously running (ok I mean myparent doesn't exit after forking subshells, and the external commands don't exit also, they are more like tail -f). In other words, the process tree would look like this:
myparent
\_ child1
\_ child2
myparent
\_ child1
\_ child2
[...]
So far so good.
Now I have a block code which is supposed to terminate all this stuff, by finding the parent processes and then the child ones, and kill them. This is it:
Code:
# find the pids of myparent processes (based on the NAMES)
PARENTS="$(ps axu | grep -e "myparent" | grep -v "grep" | awk '{ print $"2" }')"
# find the pids of the child processes.
for CANDIDATE_PROC in ${PARENTS}
do
# lets get its children (the syntax should work with all ps's)
CHILDREN_AND_PARENTS="$(ps ax --format pid,ppid,command | grep -e ${CANDIDATE_PROC} | grep -v "grep" | awk '{ print $"1" }')"
# lets sum parents and children
PROCS_TO_KILL="${PROCS_TO_KILL} ${CHILDREN_AND_PARENTS}"
done
# now lets kill all the processes
for ONE_PROC in ${PROCS_TO_KILL}
do
kill -0 "${ONE_PROC}" 2> /dev/null && kill "${ONE_PROC}"
done
And it works, most of the time. But sometimes, when I run the above code it just kills processes which aren't related to myparents or their children, destroying the apps around (like the messenger, the terminal, or even the X session).
Notes
------
1. PROCS_TO_KILL : when is used for the first time, is undeclared but I don't think this is the problem here.
2. I can't use in myparent useful tricks like: child1 & SOME_PID=$!
3. Of course there are some preliminary tests before the above code, to see if myparent is running, but I didn't include them here.
4. I thought that sometimes garbled variable content is passed to kill (like \n or other stuff), but using kill "$(echo ${ONE_PROC})" also doesn't solve this problem.
5. Could this happen because in some situations awk may exit before ps/grep finished outputing?
Any ideas why this happens and kill chooses "random" targets (processes) ?