0

I've got a C shell script that runs a program. That program spawns off a child. When I send a SIGINT to my shell script via ctrl-C, the shell script exits as well as the process that it spawned I think. However, the last process remains. How can I instruct C shell to kill all child processes before it exits then?

dromodel
  • 9,581
  • 12
  • 47
  • 65
  • 3
    Why are you writing scripts in C shell? It's the worse shell for scripting. http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ – Barmar Dec 20 '13 at 21:06
  • Yes I'm aware that it's a horrible "language" – dromodel Dec 20 '13 at 21:15
  • I (mercifully) haven't had C shell installed in years (specifically tcsh), but maybe try engaging hang up (`stty hupcl`) then a `kill -6 $$`? – bishop Dec 20 '13 at 21:21
  • 1
    It sounds like the problem is with the program not killing its child, not your script. – chepner Dec 20 '13 at 21:30

1 Answers1

1

How about creating a signal trap for SIGINT? See the Interrupt Handling section here:

#!/bin/csh

# Setup sigint handler
onintr close

# start a background process
sleep 1000&
setenv pid $!

while (1 == 1)
  echo Program is running
  sleep 2
end

close:
echo End of program. Kill $pid
kill $pid
spinlock
  • 108
  • 7