Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
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

share|improve this question
2  
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 Mar 22 '13 at 13:40
1  
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. –  Stéphane Chazelas Feb 20 at 16:11
add comment

5 Answers 5

up vote 5 down vote accepted

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

share|improve this answer
    
it says /bin/kill: -23958: No such process. –  David Dai Mar 22 '13 at 9:00
    
pkill -TERM -g PGRP, this works perfectly in my case. –  David Dai Mar 22 '13 at 9:12
add comment

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

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

pkill -TERM -P PARENT_PID
share|improve this answer
add comment

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.

share|improve this answer
    
I thought about that. And I tried /bin/kill, it didn't help. –  David Dai Mar 22 '13 at 8:59
add comment

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
'
share|improve this answer
add comment

the command you should use

 kill -TERM PID

not

 kill -TERM -PID

no minus sign (or dash) before the process id

also, killing the child processes is matter of how the author made it. If it is not a well behaving executable, it may not clean up (i.e. kill) the child processes. In which case they will have the PPID of 1; Sometimes this is by design, other times, just sloppy programming.

share|improve this answer
    
kill -TERM PID, this only kill the process PID not its child processes. –  David Dai Mar 22 '13 at 1:27
    
well, the author of that executable either deliberately or accidentally left the child processes behind in my opinion. kill command that I know, do not take negative PID as arguments. I believe you are trying to refer to the kill command described here : linux.die.net/man/3/kill if you pay attention to the verbiage, this mentions a process group id, which is not available to the command 'kill' that you issue from the command line. Don't know what to tell you. –  MelBurslan Mar 22 '13 at 1:36
    
linux.die.net/man/1/kill, as described in here, useng a negative PID is the standard way to kill a group of processes. I tried it arch linux, it worked perfectly. –  David Dai Mar 22 '13 at 1:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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