help: how does "kill -n" work?

Discussion in 'Command Line' started by fieldtrip, Jun 20, 2014.

  1. fieldtrip

    fieldtrip New Member

    Messages:
    7
    Likes Received:
    1
    Trophy Points:
    3
    Hi all,

    Let's pretend that I have a process with a PID of 12345. I have been told that if I run the following:


    Code:
    kill -n20 12345
    that kill will elevate in priority due to the assignment of the non-default nice value (via -n) and then preform a hard kill.

    My line of thinking is that a "hard kill" is kill -9, whereas the default for kill is -15. My question is that if no switch is given for the "kill level" with the above command, will it give the equivalent of a -9 or -15 kill? Furthermore, how can I test this myself? In other words, how can I kill a process and then determine whether it was killed with SIGKILL or SIGTERM?
    DevynCJohnson likes this.
  2. GrumpyOldMan

    GrumpyOldMan Active Member

    Messages:
    121
    Likes Received:
    42
    Trophy Points:
    28
    from the man page: The default signal for kill is TERM.
    JasKinasis and ryanvade like this.
  3. fieldtrip

    fieldtrip New Member

    Messages:
    7
    Likes Received:
    1
    Trophy Points:
    3
    First of all, I am wondering if I received misinformation as the man page for kill gives the following in regard to the -n switch:

    """ -n where n is larger than 1. All processes in process group
    n are signaled. When an argument of the form '-n' is
    given, and it is meant to denote a process group, either
    the signal must be specified first, or the argument must
    be preceded by a '--' option, otherwise it will be taken
    as the signal to send."""

    Secondly, echo '$&' gives the exit status of the previous command. I.E.
    Code:
    space@dimension ~/T/hey> echo '$&'
    $&
    fish: Job 2, “geany lalala” terminated by signal SIGTERM (Polite quit request)
    Furthermore, GrumpyOldMan, I appreciate your input. You may notice, however, that I included the gist of your claim in my OP. Thanks again :)
  4. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    1,227
    Likes Received:
    415
    Trophy Points:
    83
    Code:
    ~$
    kill -l
    1) SIGHUP    2) SIGINT    3) SIGQUIT    4) SIGILL    5) SIGTRAP
    6) SIGABRT    7) SIGBUS    8) SIGFPE    9) SIGKILL   10) SIGUSR1
    11) SIGSEGV   12) SIGUSR2   13) SIGPIPE   14) SIGALRM   15) SIGTERM
    16) SIGSTKFLT   17) SIGCHLD   18) SIGCONT   19) SIGSTOP   20) SIGTSTP
    21) SIGTTIN   22) SIGTTOU   23) SIGURG   24) SIGXCPU   25) SIGXFSZ
    26) SIGVTALRM   27) SIGPROF   28) SIGWINCH   29) SIGIO   30) SIGPWR
    31) SIGSYS   34) SIGRTMIN   35) SIGRTMIN+1   36) SIGRTMIN+2   37) SIGRTMIN+3
    38) SIGRTMIN+4   39) SIGRTMIN+5   40) SIGRTMIN+6   41) SIGRTMIN+7   42) SIGRTMIN+8
    43) SIGRTMIN+9   44) SIGRTMIN+10   45) SIGRTMIN+11   46) SIGRTMIN+12   47) SIGRTMIN+13
    48) SIGRTMIN+14   49) SIGRTMIN+15   50) SIGRTMAX-14   51) SIGRTMAX-13   52) SIGRTMAX-12
    53) SIGRTMAX-11   54) SIGRTMAX-10   55) SIGRTMAX-9   56) SIGRTMAX-8   57) SIGRTMAX-7
    58) SIGRTMAX-6   59) SIGRTMAX-5   60) SIGRTMAX-4   61) SIGRTMAX-3   62) SIGRTMAX-2
    63) SIGRTMAX-1   64) SIGRTMAX
    
    
    From the man page:
    Not misinformation. TERM = SIGTERM IE terminate: kill -15 = kill -TERM = kill
    Where as kill -9 = kill -SIGKILL --> hard kill

    kill -15 (kill) nicely asks the process to exit while kill -9 (SIGKILL) forces the process to close.

    http://en.wikipedia.org/wiki/SIGTERM#POSIX_signals

    -n (where n is a number greater than 0) allows you to set he elevation or "urgency" of the kill command. Either to terminate or abort the process.
    Last edited: Jun 21, 2014
    JasKinasis and DevynCJohnson like this.
  5. fieldtrip

    fieldtrip New Member

    Messages:
    7
    Likes Received:
    1
    Trophy Points:
    3
    Thanks again for the reply.

    What I meant by 'misinformation' is the whole bit about the -n switch of kill "pairing" kill with the nice command. In other words, I was told (rather, I read) that passing the -n option to kill would "combine" nice with kill with the aim of elevating the priority of the kill command above that of the priority of the process that I (or whoever) was aiming to kill. As if one could not, say, pipe nice into kill (manually).

    Again, the man page for kill mentions nothing (so far as I have seen) to declare that there is any switch/option (let alone that of -n) that will "combine" nice with kill. Therefore I am led to wonder if I have read misinformation upon that point; I came here to ask for some clarification.

    I am not confused on the fact that kill -15 = SIGTERM and that default bahaviour of kill = SIGTERM, nor am I claiming that the default behaviour of kill being SIGTERM is misinformation.
  6. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    1,227
    Likes Received:
    415
    Trophy Points:
    83
    No, kill does not use nice in any way. It merely works with the PID and the kill() system call.
    http://man7.org/linux/man-pages/man2/kill.2.html
    Last edited: Jun 21, 2014
    JasKinasis and DevynCJohnson like this.
  7. MikeyD

    MikeyD Active Member

    Messages:
    232
    Likes Received:
    112
    Trophy Points:
    43
    I found this to be a good resource for the different (or most used) SIG levels and their differences:
    http://linuxcommand.org/lc3_lts0100.php

    Specifically...

    Signal #

    Name

    Description

    1SIGHUP
    Hang up signal. Programs can listen for this signal and act upon it. This signal is sent to processes running in a terminal when you close the terminal.

    2SIGINT
    Interrupt signal. This signal is given to processes to interrupt them. Programs can process this signal and act upon it. You can also issue this signal directly by typing Ctrl-c in the terminal window where the program is running.

    15SIGTERM
    Termination signal. This signal is given to processes to terminate them. Again, programs can process this signal and act upon it. This is the default signal sent by the kill command if no signal is specified.

    9SIGKILL
    Kill signal. This signal causes the immediate termination of the process by the Linux kernel. Programs cannot listen for this signal.

Share This Page