6

I read many tutorials about the use of kill command, mostly 3 approaches

kill -15 <pid>
kill -SIGTERM <pid>
kill -TERM <pid>

For scripts purposes and for portability with macos too, the code numbers are not going to be used. Because kill -l in macos is different than Linux. So here enter in play the signal names.

Question

  • What is the correct or suggested approach to send the signal name through the kill command?

I mean:

  • SIGsomething or something?

And why? These 2 approaches exists for one reason, right? is there a mandatory reason to use an approach over the other?

Environment

This situation is for Ubuntu Desktop/Server and Fedora Workstation/Server

2
  • And your example with SIGTERM is not good (IMHO) because in Linux you can have the same result executing kill <PID> May 17 at 15:18
  • 2
    @RomeoNinov I know SIGTERM is the default signal, it was used only for demonstration purposes, it could be for either: SIGTSTP, SIGSTOP, SIGKILL, SIGCONT etc ... May 17 at 15:20

1 Answer 1

11

The most portable variant is

kill -s TERM -- …

That’s the form specified in POSIX with no extensions: -s followed by the signal’s name, as defined in signal.h, with no SIG prefix, or 0 for the “null” signal (used to check for the existence of a process with a given identifier).

SIGTERM is the default signal, so sending that to processes or process groups can be done using the following canonical forms:

kill <pid>
kill -- -<pgid>

On Linux in general, most implementations of kill (including shell builtins) accept signals as numbers or names with or without a SIG prefix; notable exceptions include the kill builtin of dash, which is the default /bin/sh in Debian-based distributions, and of the Schily Bourne Shell.

There’s no “mandatory” reason to use one form rather than another, among whatever forms are supported by the tools you use. I would personally avoid numeric forms because they can appear to be ambiguous: is kill -9 -15 intended to send SIGTERM to process groups 9 and 15, or SIGKILL to process group 15? (It’s the latter, but some readers may wonder.) I would also omit the SIG prefix since that’s not recognised everywhere.

Note that POSIX does specify a few numeric signal values:

Number Signal
0 “Null” signal
1 HUP
2 INT
3 QUIT
6 ABRT
9 KILL
14 ALRM
15 TERM
1
  • kill -9 has been a common idiom since time immemorial. Maybe it would be better to use a different example, since most other signal numbers are not well known.
    – Barmar
    May 18 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.