What does the -PIPE flag do in a kill command What does the -PIPE flag do in a kill command unix unix

What does the -PIPE flag do in a kill command


Background

kill -PIPE is telling the process that the thing it's writing to has exited, so it should exit too.

For example:

$ grep something file | more

When the more process stops reading, grep will buffer up a bit more output, but then will be paused by the kernel until more starts reading again (the size of the buffer is determined by the kernel).

Suppose there are a bazillion matches in the file; grep could keep on printing lines until the cows come home, on demand (only a few lines will get buffered, then more reads them, and when the buffer is depleted, grep will be woken up again to produce a bit more output).

But, if more exits early (the user has got bored reading the lines), there's no point for grep to keep spitting out stuff. Instead it will be sent SIGPIPE to say, "your output pipe has gone away". The default action on SIGPIPE is to terminate.

So, SIGPIPE is a weird quirk of Unix: on most platforms, if you're writing to a file and there's an error, you get an error return code produced and it's just like any other failure (that is, on Windows for instance, errors for WriteFile are indicated by the return code for the function, just like those for DeleteFile or any other Win32 function). Unix is unique, in that errors for write are handled by applications in a completely different way to errors for any other system call, through an out-of-band mechanism (signal handler). That's because pipelines are at the heart of the Unix command-line philosophy, so it's expected whenever you're writing to any pipe that you'd want special behaviour when the consumer of your stream goes away!

In your example

In your script, you're explicitly telling a process to terminate, and optionally do any cleanup it would expect to do if it were in a pipeline. Unfortunately, you haven't given us enough context to guess why your script is written that way (actually including the relevant portions of your scripts might explain the mystery). In the example with grep above though, sending SIGPIPE to grep would be a way of causing more to exit gracefully (if grep exits with SIGTERM, the parent shell might be expected to report a fiercer error, depending on how its implementation monitors the pipeline).