title: How can bash script do the equivalent of Ctrl-C to a background task?
body: <p>Is there any way to invoke a subprocess so that it and all its descendants are sent an interrupt, just as if you Ctrl-C a foreground task? I’m trying to kill a launcher script that invokes a long-running child. I’ve tried <code>kill -SIGINT $child</code> (which doesn’t send the interrupt to its descendants so is a no-op) and <code>kill -SIGINT -$child</code> (which works when invoked interactively but not when running in a script).</p>
  
  <p>Here’s a test script. The long-running script is <code>test.sh --child</code>. When you call <code>test.sh --parent</code>, it invokes <code>test.sh --child &amp;</code> and then tries to kill it. How can I make the parent kill the child successfully?</p>
  
  <pre><code>#!/bin/bash
  
  if [ "$1" = "--child" ]; then
  sleep 1000
  
  elif [ "$1" = "--parent" ]; then
  "$0" --child &amp;
  for child in $(jobs -p); do
    echo kill -SIGINT "-$child" &amp;&amp; kill -SIGINT "-$child"
  done
  wait $(jobs -p)
  
  else
  echo "Must be invoked with --child or --parent."
  fi
  </code></pre>
  
  <p>I know that you can modify the long-running child to <code>trap</code> signals, send them to its subprocess, and then wait (from 
  <a href="https://stackoverflow.com/questions/11696691/bash-script-kill-background-grandchildren-on-ctrlc">Bash script kill background (grand)children on Ctrl+C</a>), but is there any way without modifying the child script?</p>