killing child processes of a bash script results in strange random kills
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) 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) ? |
Maybe use session ID's?
Code:
echo "Hello, I am $$."; for pid in `pgrep -s $$`; do |
Excellent. Thank you, you gave me 2 good ideas - to use pgrep (or directly pkill) which will reduce all the above code to maybe only 2 lines, and to use session ID's - so that only the procs in this range will be killed. I'm quite sure this will fix it - too bad I can't find a decent explanation why the old code was failing *sometimes* on linux (I use a hardened kernel btw (PAX+GRSEC (with filesystem and proc protection and randomisation)). Thanks again.
|
too bad I can't find a decent explanation why the old code was failing *sometimes* on linux (I use a hardened kernel btw (PAX+GRSEC (with filesystem and proc protection and randomisation)).
Well you could run some debug runs (set -x) and look at the output, maybe it'll explain why. If not maybe post output from a failed debug run here (in BB "code" tags please). I strongly doubt GRSec or PAX are to blame for any of this. If they where you'd seen stuff in the logs, or so I'd hope. |
One explanation I can think of is related to:
Code:
grep -e ${CANDIDATE_PROC} Code:
grep -e " ${CANDIDATE_PROC} " |
Quote:
grep -e "[m]yparent" it seems you are going to a lot of trouble here - for what reason? ;) |
Quote:
Quote:
|
All times are GMT -5. The time now is 09:14 AM. |