Kocil
asked on
Killing all off-spring processes
Dear experts.
I want to make a MAIN bash script that spawns several processes.
The spawned processes then may spawn other processes.
If the MAIN script exit, all of the spawned processes must be killed.
How to do that ?
I have tried to put "set -m" in the front of the MAIN script,
but it is not working.
Thanks.
I want to make a MAIN bash script that spawns several processes.
The spawned processes then may spawn other processes.
If the MAIN script exit, all of the spawned processes must be killed.
How to do that ?
I have tried to put "set -m" in the front of the MAIN script,
but it is not working.
Thanks.
ASKER
Hi. Thanks for the respond.
But I could not find pids on my system (Vector Linux).
Seeking it on google find a lot of pids but nothing close for this problem.
Let me restate my goal. Here is the situation.
- login-shell.
+---startx
+---X-init
+---WM-menu
+---my-MAIN-script
+---my-child-1
+---my-child-2
+---my-grand-child-21
+---my-grand-child-22
+---my-grand-child-2N
+---my-child-M
So basically, I want to launch X-GUI with a WM-menu.
The users able to choose a WM (windows manager).
The WM-Menu then will launch the MAIN script, which will launch various programs (child 1...M).
When the user quit, I want that the MAIN script will kill all of the child, grand-child,
grand-grand child etc ... then return back to the WM-Menu.
Here is my not-working-yet MAIN-script (for launching XFCE4)
----------------------
[code]
#!/bin/sh
#Launching XFCE4 with goodies
# this supposed to be set this process as group leader
#but no effect so far
set -m
# launch the goodies
xfce-iconbox
xfdesktop &
# launch the main panel
xfce4-panel
# the panel exit on user command
# during the session the user may launch any application using the xfce4-panel or the xfdesktop.
# oddly enough, XFCE4 don't kill the applications.
# this kills the goodies
skill xfce-iconbox
skill xfce-xfdesktop
# this was only checking what process still running
ps
# the script exit
exit 0
# I expect that the applications would be killed after exiting
# but I found the applications were still hanging around
# messing with the WM-Menu.
[/code]
---------------
Thanks for your help and patience.
But I could not find pids on my system (Vector Linux).
Seeking it on google find a lot of pids but nothing close for this problem.
Let me restate my goal. Here is the situation.
- login-shell.
+---startx
+---X-init
+---WM-menu
+---my-MAIN-script
+---my-child-1
+---my-child-2
+---my-grand-child-21
+---my-grand-child-22
+---my-grand-child-2N
+---my-child-M
So basically, I want to launch X-GUI with a WM-menu.
The users able to choose a WM (windows manager).
The WM-Menu then will launch the MAIN script, which will launch various programs (child 1...M).
When the user quit, I want that the MAIN script will kill all of the child, grand-child,
grand-grand child etc ... then return back to the WM-Menu.
Here is my not-working-yet MAIN-script (for launching XFCE4)
----------------------
[code]
#!/bin/sh
#Launching XFCE4 with goodies
# this supposed to be set this process as group leader
#but no effect so far
set -m
# launch the goodies
xfce-iconbox
xfdesktop &
# launch the main panel
xfce4-panel
# the panel exit on user command
# during the session the user may launch any application using the xfce4-panel or the xfdesktop.
# oddly enough, XFCE4 don't kill the applications.
# this kills the goodies
skill xfce-iconbox
skill xfce-xfdesktop
# this was only checking what process still running
ps
# the script exit
exit 0
# I expect that the applications would be killed after exiting
# but I found the applications were still hanging around
# messing with the WM-Menu.
[/code]
---------------
Thanks for your help and patience.
$! only returns the pid of the last backgrounded process
Kocil, there is no direct way (that I know of) of doing what you are looking for from the shell
the only way that I can think of is to do a ps, extract all entries with the name of your program and do an explicit kill for all of them... but this raises the issue of running multiple instances of our program
Kocil, there is no direct way (that I know of) of doing what you are looking for from the shell
the only way that I can think of is to do a ps, extract all entries with the name of your program and do an explicit kill for all of them... but this raises the issue of running multiple instances of our program
ASKER
Thanks sunny.
In that situation,
suppose I call ps on the end of the script.
We will gave someting like this, right
PID TTY TIME CMD
....
...
9999 ttt 00:00 main-script
....
....
I can parse it to get the PID and kill them.
But the question is,
how we can determines the child processes ?
Is that guaranted that the ROWS after the
main-script are the childs ?
I found that the PIDs do not always not ordered,
it will roll back after 32xyz.
Thanks.
In that situation,
suppose I call ps on the end of the script.
We will gave someting like this, right
PID TTY TIME CMD
....
...
9999 ttt 00:00 main-script
....
....
I can parse it to get the PID and kill them.
But the question is,
how we can determines the child processes ?
Is that guaranted that the ROWS after the
main-script are the childs ?
I found that the PIDs do not always not ordered,
it will roll back after 32xyz.
Thanks.
ASKER
>> We will gave someting like this, right.
Syntax error. Should be
We will get ...
Syntax error. Should be
We will get ...
PID TTY TIME CMD
....
...
9999 ttt 00:00 main-script
....
....
>Is that guaranted that the ROWS after the
>main-script are the childs ?
No
>>how we can determines the child processes ?
As far as I remember, they have the same CMD field ... pls check
....
...
9999 ttt 00:00 main-script
....
....
>Is that guaranted that the ROWS after the
>main-script are the childs ?
No
>>how we can determines the child processes ?
As far as I remember, they have the same CMD field ... pls check
ASKER
Do you mean CMD the 4th field ?
That field does not say something about parent - child relations.
That field does not say something about parent - child relations.
>That field does not say something about parent - child relations
I agree but children are launched with the same name ... If you are simply creating multiple processes to do a task in parallel, this should suffice
I agree but children are launched with the same name ... If you are simply creating multiple processes to do a task in parallel, this should suffice
just checked the man page of ps ... there is option of getting ppid using %p specifier ... but it will be good amount of coding using that method
something like
get your process id ... get all processes with ppid as your process id .. continue recursively until a search does not turn up a new process ...
Just thought of another way ... execute a setsid in the topmost process ... it will become session leader
then a kill with -n flag will send a signal to all the processes in this group and it includes all the children, grand children etc (unless you are doing a setsid in one of them and setting a new leader)
get your process id ... get all processes with ppid as your process id .. continue recursively until a search does not turn up a new process ...
Just thought of another way ... execute a setsid in the topmost process ... it will become session leader
then a kill with -n flag will send a signal to all the processes in this group and it includes all the children, grand children etc (unless you are doing a setsid in one of them and setting a new leader)
ASKER
Unfortunatelly, I have cecked it again, XFCE launches apps as its own setsid.
The parent is 1 (init). Here is the result from ps -H.
=========
PID TTY TIME CMD
697 ? 00:00:00 xfe
695 ? 00:00:00 nedit
690 ? 00:00:00 aterm
693 ttyp0 00:00:00 bash
683 ? 00:00:00 dfm
678 ? 00:00:00 xfce-mcs-manage
652 ? 00:00:00 kapmd
578 tty6 00:00:00 bash
637 tty6 00:00:00 startx
649 tty6 00:00:00 xinit
650 ? 00:00:02 X
662 tty6 00:00:00 XwmMenu
670 ? 00:00:00 xinitrc.xfce4
679 ? 00:00:00 xfwm4
680 ? 00:00:00 xfbackdrop
682 ? 00:00:00 xfce4-panel
======
What a life ...
Fluxbox, WindowsMaker, and XFCE4 have the same problem.
But IceWM and KDE do kill the applications.
The parent is 1 (init). Here is the result from ps -H.
=========
PID TTY TIME CMD
697 ? 00:00:00 xfe
695 ? 00:00:00 nedit
690 ? 00:00:00 aterm
693 ttyp0 00:00:00 bash
683 ? 00:00:00 dfm
678 ? 00:00:00 xfce-mcs-manage
652 ? 00:00:00 kapmd
578 tty6 00:00:00 bash
637 tty6 00:00:00 startx
649 tty6 00:00:00 xinit
650 ? 00:00:02 X
662 tty6 00:00:00 XwmMenu
670 ? 00:00:00 xinitrc.xfce4
679 ? 00:00:00 xfwm4
680 ? 00:00:00 xfbackdrop
682 ? 00:00:00 xfce4-panel
======
What a life ...
Fluxbox, WindowsMaker, and XFCE4 have the same problem.
But IceWM and KDE do kill the applications.
ASKER
I raised the points to 500.
Please help ...
Please help ...
myPID=`awk '/^PPid/{print $2}' /proc/self/status`
kill ps axl|awk '($4 == "'$PiD'"){print $3}'
# to be improved in many way (returns at least 2 pids which are no longer existing)
kill ps axl|awk '($4 == "'$PiD'"){print $3}'
# to be improved in many way (returns at least 2 pids which are no longer existing)
opps, typo
pleace replace PiD by myPID
pleace replace PiD by myPID
On solaris we have option to show pid and ppid inthe same line.
u can capture ppid of the script once the script starts. with invoking
script_ppid= ps -aef | grep script_name and capturing the pid field. ( this must the first column).
and when u try to kill it , just get the ppid field and try to match with pid of the above and kill them all.
or u can do more thing. all these processes and sub processes might be having the same
tty filed.
so get the first ppid with script_name and kill everything aasociated with this tty except the ppid one.
u can capture ppid of the script once the script starts. with invoking
script_ppid= ps -aef | grep script_name and capturing the pid field. ( this must the first column).
and when u try to kill it , just get the ppid field and try to match with pid of the above and kill them all.
or u can do more thing. all these processes and sub processes might be having the same
tty filed.
so get the first ppid with script_name and kill everything aasociated with this tty except the ppid one.
ASKER
I login as user vec on the tty2 and tty6. Then startx from tty6.
Here is the more complete ps output.
PID PGID SID TTY TIME CMD
2421 2421 2421 ? 00:00:00 nedit
2381 2381 2381 ? 00:00:00 run-mozilla.sh
2388 2381 2381 ? 00:00:17 MozillaFirebird
2390 2381 2381 ? 00:00:00 MozillaFirebird
2391 2381 2381 ? 00:00:00 MozillaFirebird
2392 2381 2381 ? 00:00:00 MozillaFirebird
2393 2381 2381 ? 00:00:00 MozillaFirebird
2394 2381 2381 ? 00:00:00 MozillaFirebird
2356 2356 2356 tty2 00:00:00 bash
2368 2368 2356 tty2 00:00:00 mc
2371 2371 2371 ttyp0 00:00:00 bash
2348 2348 2348 ? 00:00:00 xfe
2324 2321 578 tty6 00:00:00 xterm
2326 2326 2326 ttyp2 00:00:00 bash
2322 2320 578 tty6 00:00:00 dfm
2318 2317 578 tty6 00:00:00 xfwm4
2316 2316 2316 ? 00:00:00 xfce-mcs-manage
1841 1 1 ? 00:00:00 kapmd
578 578 578 tty6 00:00:00 bash
1827 1827 578 tty6 00:00:00 startx
1838 1827 578 tty6 00:00:00 xinit
1839 1839 578 ? 00:00:37 X
1851 1851 578 tty6 00:00:00 XwmMenu
2305 1851 578 tty6 00:00:00 xinitrc.xfce4
2319 2319 578 tty6 00:00:00 xfbackdrop
2321 2321 578 tty6 00:00:00 xfce4-panel
=========================
Please examine that the XwmMenu and xinitrc.xfce4 have
the same PID and SID.
However, the xfce-mcs-manager and also other apps (mozilla, xft, xterm)
have different PID and SID. They even do not have TTY.
Here is the more complete ps output.
PID PGID SID TTY TIME CMD
2421 2421 2421 ? 00:00:00 nedit
2381 2381 2381 ? 00:00:00 run-mozilla.sh
2388 2381 2381 ? 00:00:17 MozillaFirebird
2390 2381 2381 ? 00:00:00 MozillaFirebird
2391 2381 2381 ? 00:00:00 MozillaFirebird
2392 2381 2381 ? 00:00:00 MozillaFirebird
2393 2381 2381 ? 00:00:00 MozillaFirebird
2394 2381 2381 ? 00:00:00 MozillaFirebird
2356 2356 2356 tty2 00:00:00 bash
2368 2368 2356 tty2 00:00:00 mc
2371 2371 2371 ttyp0 00:00:00 bash
2348 2348 2348 ? 00:00:00 xfe
2324 2321 578 tty6 00:00:00 xterm
2326 2326 2326 ttyp2 00:00:00 bash
2322 2320 578 tty6 00:00:00 dfm
2318 2317 578 tty6 00:00:00 xfwm4
2316 2316 2316 ? 00:00:00 xfce-mcs-manage
1841 1 1 ? 00:00:00 kapmd
578 578 578 tty6 00:00:00 bash
1827 1827 578 tty6 00:00:00 startx
1838 1827 578 tty6 00:00:00 xinit
1839 1839 578 ? 00:00:37 X
1851 1851 578 tty6 00:00:00 XwmMenu
2305 1851 578 tty6 00:00:00 xinitrc.xfce4
2319 2319 578 tty6 00:00:00 xfbackdrop
2321 2321 578 tty6 00:00:00 xfce4-panel
=========================
Please examine that the XwmMenu and xinitrc.xfce4 have
the same PID and SID.
However, the xfce-mcs-manager and also other apps (mozilla, xft, xterm)
have different PID and SID. They even do not have TTY.
ASKER
Continue ...
So I still can not see the way to implement your ideas.
Is it posible to do this from C ?
If yes, then I'll hack the XwmMenu it self.
So I still can not see the way to implement your ideas.
Is it posible to do this from C ?
If yes, then I'll hack the XwmMenu it self.
did you try my suggestion?
please post result of:
\ps lax
please post result of:
\ps lax
ASKER
I did.
It did not work.
It killed only xfbackdrop.
As you see on my ps output before (sorry, ps lax is not available yet. Must rebooting to get it :).
only xfbackdrop and xfce4-panel have the parent of xinitrc.xfce4 (the script).
The other XFCE components (xfce-mcs-manager and xfwm) have their own SID and PGID with PID=1 (init).
Also the applications (dfm, xfe, xterm, mozilla) have PGID = 1.
It did not work.
It killed only xfbackdrop.
As you see on my ps output before (sorry, ps lax is not available yet. Must rebooting to get it :).
only xfbackdrop and xfce4-panel have the parent of xinitrc.xfce4 (the script).
The other XFCE components (xfce-mcs-manager and xfwm) have their own SID and PGID with PID=1 (init).
Also the applications (dfm, xfe, xterm, mozilla) have PGID = 1.
if there have been processes spawned using fork and exec, so they get 1 as PPID, then there is no possibility to identify which PID was their real father.
You either need to keep a list of your spawned processes, or kill them by name (means you're shure that you don't need them)
You either need to keep a list of your spawned processes, or kill them by name (means you're shure that you don't need them)
ASKER
>> You either need to keep a list of your spawned processes.
Well, then only xfce it self can kill the right processes.
I wonder why it does not.
Anyway ... here is the complete ps -lax if you are still interested.
F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND
4 1001 572 1 8 0 2272 1308 pipeli S tty1 0:00 -bash
4 1001 578 1 9 0 2272 1308 proc_d S tty6 0:00 -bash
0 1001 684 578 9 0 2028 980 proc_d S tty6 0:00 /bin/sh /usr/X11R6/bin/startx
0 1001 695 684 9 0 2224 640 proc_d S tty6 0:00 xinit /home/vec/.xinitrc --
0 1001 708 695 9 0 3408 1452 proc_d S tty6 0:00 /usr/bin/XwmMenu
0 1001 710 708 8 0 2024 964 proc_d S tty6 0:00 /bin/sh /etc/X11/xinit/xinitrc.xfc e4
1 1001 721 1 9 0 10136 4528 alloc_ S ? 0:00 xfce-mcs-manager
1 1001 723 1 9 0 8276 4632 alloc_ S tty6 0:00 xfwm4 --daemon
0 1001 724 710 9 0 9264 4272 alloc_ S tty6 0:00 xfbackdrop
0 1001 726 710 9 0 10848 6916 alloc_ S tty6 0:00 xfce4-panel
1 1001 727 1 11 0 5812 2676 end_ki S tty6 0:00 dfm
0 1001 729 1 9 0 5936 3560 end_ki S ? 0:00 /usr/X11R6/bin/nedit
0 1001 731 1 9 0 8656 4516 end_ki S ? 0:00 /usr/X11R6/bin/xfe
Well, then only xfce it self can kill the right processes.
I wonder why it does not.
Anyway ... here is the complete ps -lax if you are still interested.
F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND
4 1001 572 1 8 0 2272 1308 pipeli S tty1 0:00 -bash
4 1001 578 1 9 0 2272 1308 proc_d S tty6 0:00 -bash
0 1001 684 578 9 0 2028 980 proc_d S tty6 0:00 /bin/sh /usr/X11R6/bin/startx
0 1001 695 684 9 0 2224 640 proc_d S tty6 0:00 xinit /home/vec/.xinitrc --
0 1001 708 695 9 0 3408 1452 proc_d S tty6 0:00 /usr/bin/XwmMenu
0 1001 710 708 8 0 2024 964 proc_d S tty6 0:00 /bin/sh /etc/X11/xinit/xinitrc.xfc
1 1001 721 1 9 0 10136 4528 alloc_ S ? 0:00 xfce-mcs-manager
1 1001 723 1 9 0 8276 4632 alloc_ S tty6 0:00 xfwm4 --daemon
0 1001 724 710 9 0 9264 4272 alloc_ S tty6 0:00 xfbackdrop
0 1001 726 710 9 0 10848 6916 alloc_ S tty6 0:00 xfce4-panel
1 1001 727 1 11 0 5812 2676 end_ki S tty6 0:00 dfm
0 1001 729 1 9 0 5936 3560 end_ki S ? 0:00 /usr/X11R6/bin/nedit
0 1001 731 1 9 0 8656 4516 end_ki S ? 0:00 /usr/X11R6/bin/xfe
hmm, can we make the rule that you want to kill anything startet from bash on tty6?
kill `ps -lax|grep -v bash|awk '($11=="tty6"){print $3}'`
# works with gawk, but not traditional AT&T awk, please check yourself
kill `ps -lax|grep -v bash|awk '($11=="tty6"){print $3}'`
# works with gawk, but not traditional AT&T awk, please check yourself
ASKER
Sorry man.
I want to kill any process launched by XwmMenu and it's offspring.
Your solution:
- kill the X, so the user back to the console.
- does not kill the X-programs that do not use TTY, but actually the offspring
Thanks.
PS: I can't believe this simple wish is so hard to achieve.
I want to kill any process launched by XwmMenu and it's offspring.
Your solution:
- kill the X, so the user back to the console.
- does not kill the X-programs that do not use TTY, but actually the offspring
Thanks.
PS: I can't believe this simple wish is so hard to achieve.
Sorry, I had been a bit occupied these days and did not check back on this thread as it needed more time
>I want to kill any process launched by XwmMenu and it's offspring.
>Is it posible to do this from C ?
This simple wish will indeed be hard to achieve and will require some work
The easiest way(s) that I can think of is
1. In C, each process when created writes its pid to a file and on exit removes it from the file (man atexit) ... Most likely you would be kiling the parent using some signal ... in that signal handler, read that file and send kill signals to all processes with the PIDs listed in that file
2. In C, whenever you do a setsid, note down the pid of the group leader and send kills to -pid (man 2 kill)
If these sound reasonable to you, we can discuss them in detail.
>I want to kill any process launched by XwmMenu and it's offspring.
>Is it posible to do this from C ?
This simple wish will indeed be hard to achieve and will require some work
The easiest way(s) that I can think of is
1. In C, each process when created writes its pid to a file and on exit removes it from the file (man atexit) ... Most likely you would be kiling the parent using some signal ... in that signal handler, read that file and send kill signals to all processes with the PIDs listed in that file
2. In C, whenever you do a setsid, note down the pid of the group leader and send kills to -pid (man 2 kill)
If these sound reasonable to you, we can discuss them in detail.
sunnycoder, see my comment
Date: 12/15/2003 11:50PM CET
Date: 12/15/2003 11:50PM CET
ASKER
Dear Sunny and ahoffman.
I nearly give up :)
I sent the problem to xfce-milist.
The developers wonder why do people need those feature
and said to forget it.
They suggested "Don't to use XwmMenu, instead, use kdm, gdm or xdm that kill everything".
I don't like that idea since that mean I have to logout and login again each time I want to change the WM/DE.
But what can I say ... :(
I nearly give up :)
I sent the problem to xfce-milist.
The developers wonder why do people need those feature
and said to forget it.
They suggested "Don't to use XwmMenu, instead, use kdm, gdm or xdm that kill everything".
I don't like that idea since that mean I have to logout and login again each time I want to change the WM/DE.
But what can I say ... :(
oops ... sorry ahoffmann ... I had lost track of the thread ... will be more careful later...
Kocil, it really is uncommon to kill a process and all its offsprings if you are making an offspring a group leader. It defeats the entire purpose of making a separate group leader.
Kocil, it really is uncommon to kill a process and all its offsprings if you are making an offspring a group leader. It defeats the entire purpose of making a separate group leader.
ASKER
>> ... if you are making an offspring a group leader.
Not me ... It's them.
I'm the victim. :)
Not me ... It's them.
I'm the victim. :)
victim? use more reliable and simple tools than xfce*:)
SCNR
SCNR
ASKER
No solution for this problem :(
Does anybody complain If I cancel this question ?
Does anybody complain If I cancel this question ?
yes and no :o)
yes because there is no solution and all possible avenues were exhausted ... I concede that I lost track for a while but others did not
no because we indeed were not much help in the given situation
Go ahead, post in CS for deletion ... no objections from my side
yes because there is no solution and all possible avenues were exhausted ... I concede that I lost track for a while but others did not
no because we indeed were not much help in the given situation
Go ahead, post in CS for deletion ... no objections from my side
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
kill $pids