Killing csh background process **AND** any unfinished processes (fg and background) it started

Killing csh background process **AND** any unfinished processes (fg and background) it started

Post by jurge » Fri, 07 Sep 2001 14:10:17



Hi,

Does anybody know of a solution for killing a csh background process
**AND** any unfinished processes (fg and background) it started.

I know you guys think tcsh sucks but translating the csh code to bash is
not trivial task because of it's large volume of the case I have at
hand.

I should mention that when the csh script is started in the foreground
on a terminal and I do a ^C it behaves excellent in that it exits and
all the processes it started exit.

Things I considered (and didn't work for me):

   * Use the -q option of csh and kill with a signal: QUIT (3). Only
     kills the shell but not a process started by the shell in the
     foreground (actually another csh).
   * Use the csh onintr command and when interrupt time arrives the
     processes will be killed with signal INT (2) before the script
     exits. The same for the processes started. With the many scripts
     this becomes unfeasible. I'm hoping to add a line or two to each
     script, max.
   * Use the tcsh hup command and kill with a signal: HUP (1). Again,
     the process started by tcsh in the fg doesn't finish.
   * Set limits on cpu usage. Ugly and insufficient because a process
     might not take cpu but still get left behind.

After this mess is solved, I'll try to hook this all up to Python;-) And
never code in csh again?

Thanks for your time,
Jurgen

 
 
 

Killing csh background process **AND** any unfinished processes (fg and background) it started

Post by Jurgen F. Doreleijer » Sat, 08 Sep 2001 01:27:07


I knew it, one of the very simple answers is using a negative pid as
an argument to a kill command in the calling shell. The negative pid
indicates to kill that all the processes with the given **group** id
should be killed. This works great for me.

Thanks to Greg for noting it to me,
Jurgen

--
    Jurgen F. Doreleijers
    BioMagResBank, Univ. of Wisconsin-Madison, WI, USA

    http://urchin.bmrb.wisc.edu/~jurgen

 
 
 

Killing csh background process **AND** any unfinished processes (fg and background) it started

Post by Bruce Barnet » Sat, 08 Sep 2001 06:03:10



> After this mess is solved, I'll try to hook this all up to Python;-) And
> never code in csh again?

You don't have to be THAT extreme. Just use the Bourne shell.

(This is one of those reasons we tell people to avoid the C shell.)

-----------------------
#!/bin/sh
V=

# launch 3 children

/tmp/b & V="$V $!"
/tmp/b & V="$V $!"
/tmp/b & V="$V $!"

echo CHILDREN processes are $V
echo "to abort, type 'kill -1 $$'"
trap 'echo "caught signal";kill -1 $V;wait;exit' 1

wait
echo all children done
exit 0
-----------------------
and /tmp/b is
-----------------------
#!/bin/sh

trap "echo 'Process $$ caught signal 1, exiting...';exit" 1

sleep 10

echo process $$ exit normally
exit 0
-----------------------

--
Bruce  <barnett at crd. ge. com> (speaking as myself, and not a GE employee)

 
 
 

Killing csh background process **AND** any unfinished processes (fg and background) it started

Post by Jurgen F. Doreleijer » Sun, 09 Sep 2001 01:09:08


I would like to be gentle to my processes in general indeed!

However, in my project the children are not put in the background and
it's not feasible to do that for all of them (in my project). The
solution you suggest would not work in then, right? Any better way
though?

Currently I fixed it with doing the kill -1, -pid for killing the
group but it's quite severe and a hassle because the child needs to
have it's own group id which it doesn't get automatically with fork
(in Python).

Thanks for your thoughts!
Jurgen

--
    Jurgen F. Doreleijers
    BioMagResBank, Univ. of Wisconsin-Madison, WI, USA

    http://urchin.bmrb.wisc.edu/~jurgen

 
 
 

1. Capturing the Process ID of a Process started in Background in ksh

I believe that from the above code things should work as you expected.

The only thing I can think is that somehow a subshell was forked to
run the tail command.  Maybe you can look at what process '2433' is?

Can you post a complete script which shows the problem? Please edit
the script down before posting to give "the smallest script which
still exhibits the problem" Nobody'll bother reading anything more
than ten lines long :-)

Alexis

2. A weird post-recompile effect and a Newbie question.

3. Process id of process started in "background" from script

4. Y2K Question on Solaris 2.6 (Beginner)

5. remotely started background process gets killed

6. Veritas or UFS

7. How do I disassociate a background process from the process used to start it?

8. LICQ, download manager for linux, RPM

9. Q: Background processes from csh script?

10. PID of background process in csh

11. Get Last Background Process Number in csh

12. csh background processes

13. Background process is killed when user logs off on AIX