linuxsigintdddash-shell

How to close a dd process that was started in a dash script with -SIGINT?


When I start a dd process directly from the Terminal with

dd if=/dev/zero of=/dev/null &

command, and send to it a -SIGINT with

kill -SIGINT <pid>

command, it closes successfully.

But when I start the process from a script

#!/bin/sh
dd if=/dev/zero of=/dev/null &

Then do

kill -SIGINT <pid>

it doesn't affect the process.

I wonder why this is so.
I didn't find any related information on the internet.


Solution

  • POSIX says:

    If job control is disabled (see the description of set -m) when the shell executes an asynchronous list, the commands in the list shall inherit from the shell a signal action of ignored (SIG_IGN) for the SIGINT and SIGQUIT signals.

    This is likely because Ctrl+C sends a sigint to every process in the group, so without this behavior, any backgrounded processes would unexpectedly be killed when you try to interrupt the main script.