kill a group of processes with negative PID

23,043

Solution 1

Does it say "no such PID" or is there an error, - as in does this work?

kill -TERM -- -GPID

Also note, as per (emphasize mine)
man 1:

"[…] When an argument of the form '-n' is given, and it is meant to denote a process group […]"

man 2:

"[…] If pid is less than -1, then sig is sent to every process in the process group whose ID is -pid. […]"

man 3:

"[…] If pid is negative, but not -1, sig shall be sent to all processes (excluding an unspecified set of system processes) whose process group ID is equal to the absolute value of pid, […]"

As in, not PID but process group ID.


Else perhaps you can have so fun with /proc/[pid]/stat

ppid: awk '{gsub(/\([^)]+\)/,"_"); print $4}' /proc/3955/stat
pgrp: awk '{gsub(/\([^)]+\)/,"_"); print $5}' /proc/3955/stat

pkill -TERM -g PGRP

Solution 2

The error message /bin/kill: -23958: No such process may also be due to the fact that the pid 23958 is no pgid (process group id number)!

This can, for example, be the case if you try to kill a backgrounded shell (or command) in a script mistakenly using $! as a pgid; in a job-control enabled shell, however, $! can be used as a pgid (see: Why child process still alive after parent process was killed in Linux?).

# examples of how to kill a process group
# (using sh -c '...' instead of a shell script)

# kill: -<num>: No such process
sh -c '
(sleep 200 & sleep 200 & sleep 200) &
/bin/kill -s TERM -$!
'

# in Terminal.app 
# job control is enabled by default
(sleep 200 & sleep 200 & sleep 200) &
/bin/kill -s TERM -$!


# enable job control
sh -c '
set -m 
(sleep 200 & sleep 200 & sleep 200) &
/bin/kill -s TERM -$!
'

# make sure pid == pgid
# note that the script gets killed as well
sh -c '
echo pid $$
trap "trap - EXIT; echo pid $$ was killed" EXIT
(sleep 200 & sleep 200 & sleep 200) &
IFS=" " read -r pgid <<EOF
$(ps -p $! -o pgid=)
EOF
sleep 5
/bin/kill -s TERM -${pgid}
sleep 5
echo pid $$ was not killed
'

# use a pseudo-terminal (pty) to avoid killing the script
# note the use of -$$ instead of -$!
sh -c '
echo pid $$
script -q /dev/null sh -c '\''
trap "" HUP
(sleep 200 & sleep 200 & sleep 200) &
sleep 5
/bin/kill -s TERM -$$
'\''
sleep 5
echo pid $$ was not killed
'

Solution 3

This may have to do with you using the shell built-in kill and not the binary, try to use

/bin/kill 

If it works I would suggest to check if your shell has added the process group feature to a newer version. Or just use the binary.

Solution 4

if the underlying OS uses Busybox and thus, says Error: kill: bad signal name '-', it's most probably an issue with --.

You need to specify kill -TERM -- -[gpid] instead of just kill -- -[gpid]. TERM is the default signal.

I dont know why.

Share:
23,043

Related videos on Youtube

SparedWhisle
Author by

SparedWhisle

Updated on September 18, 2022

Comments

  • SparedWhisle
    SparedWhisle over 1 year
    kill -TERM -PID 
    

    is supposed to kill PID and all its child processes.
    but this doesn't work on openSUSE, it always tell me that no such process -PID no matter what PID i use.
    So if the negative PID option is not supported by this particular version of kill, what is the best way to kill a group of processes?

    background:
    I have a shell script running. inside the script, I use wget to download things. So the script is the parent process, wget is the child process. I want to kill them both using kill -TERM -PID_OF_SCRIPT

    • Admin
      Admin about 11 years
      Quoting the standard: If the first pid operand is negative, it should be preceded by "--" to keep it from being interpreted as an option. Also, using a negative number for the pid arg actually refers to the process group ID.
    • Admin
      Admin about 10 years
      It's not supposed to kill PID and all its child processes, it's supposed to kill all the processes of pgid PID. Use ps -j to see the process group ids.
  • SparedWhisle
    SparedWhisle about 11 years
    it says /bin/kill: -23958: No such process.
  • Zibri
    Zibri over 4 years
    what does kill -- do?
  • Tobia
    Tobia over 3 years
    @Zibri usually (by convention) a single argument of -- stops the processing of "options" and reads all words after the -- as "arguments." For example, to remove a file that is called -r you cannot use rm -r, you need to write rm -- -r. I assume this is the same with kill. If you need to kill the process group id 9, you will have to write kill -- -9 because kill -9 treats the number as an option (the signal to use) instead of an argument (the process or group id to kill.)