7
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

2
  • 3
    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.
    – Totor
    Commented Mar 22, 2013 at 13:40
  • 2
    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. Commented Feb 20, 2014 at 16:11

5 Answers 5

9

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

4
  • 1
    it says /bin/kill: -23958: No such process. Commented Mar 22, 2013 at 9:00
  • pkill -TERM -g PGRP, this works perfectly in my case. Commented Mar 22, 2013 at 9:12
  • what does kill -- do?
    – Zibri
    Commented Aug 8, 2019 at 16:21
  • @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.)
    – Tobia
    Commented Dec 1, 2020 at 18:29
6

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
'
2

You could use the "pkill" or the "killtree" solution, shown on this thread on other of the StackExchange sites :)

https://stackoverflow.com/questions/392022/best-way-to-kill-all-child-processes

pkill -TERM -P PARENT_PID
2

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.

1
  • I thought about that. And I tried /bin/kill, it didn't help. Commented Mar 22, 2013 at 8:59
1

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.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .