You are not logged in.
I decided to make a session manager that will kill all child processes before exiting, the only problem is that it doesn't kill them all.
I tried using the same function, and using it in conjunction with
yes >/dev/null & bash
yes >/dev/null & bash
yes >/dev/null & bash
yes >/dev/null & bash
running in a terminal, and it sent all of them the TERM signal.
I'm out of ideas.
Code is here.
The relevant section is:
# Kill child process of given parent
# This uses PIDs not names
# Also prints out a list of child pids that should be killed
# FIXME: Clean this up a bit
kill_child_processes()
{
PARENT_ID=$1
# Find child processes and put them in the form PID%seperator%COMMAND
local CHILD_PROCESSES=$(ps o ppid,pid,command | \
awk "{if (\$1 == $PARENT_ID) {print \$2,\"_\",\$3;}}" | \
sed 's| _ |%seperator%|')
# Do this for each child
for child in $CHILD_PROCESSES; do
# Extract the PID and COMMAND into separate variable from $child
local child_pid=$(echo $child | sed 's|%seperator%.*||')
local child_command=$(echo $child | sed 's|.*%seperator%||')
# If the child command has child processes, recurse through it
if [ "$(ps o ppid,pid,command | awk "{if (\$1 == $child_pid) {print \$2;}}")" != "" ]; then
kill_child_processes $child_pid
fi
# If the command isn't part of a blacklist, send it the TERM signal
if [ x$(for dont_kill_command in $DONT_TERM; do \
[ x$dont_kill_command == x$child_command ] && echo "MATCH"; \
done) == x"" ]; then
kill $child_pid &
# echo the PID so we can use it later
echo $child_pid
fi
done
}
Thanks.
Last edited by some-guy94 (2010-01-13 22:48:39)
Offline
You might like to try pkill / pgrep with the -g option?
larch: http://larch.berlios.de
Offline
You might like to try pkill / pgrep with the -g option?
Unfortunately that isn't what I'm looking for, I want this to work with multiple sessions(not that difficult), and also work with other apps on other tty's.
Currently what it does is it runs ps o ppid,pid,command x which gives a nice table
1234 5678 command1
1234 5679 command2
...
and if the ppid matches the session's pid, it's pid is saved, and (this part is looped) if it has child processes, then the function is run on the pid, afterward the pid is killed.
It works when I separate the function from the rest of the script, but when it is in a session it fails to work properly.
Last edited by some-guy94 (2010-01-05 23:11:57)
Offline
I solved this by changing all the 'ps [args]' to 'ps [args] x'.
I can't believe I forgot that.
Thanks for all the help.
Offline