does linux kill command terminate child processes?
When you i开发者_如何学Cnvoke "kill" on a parent process, are the child processes subsequently killed as well?
No, not automatically.
Commonly, when a parent is killed the child will be sent a HUP signal. At least this is true when the parent is a shell. I'm not sure about if this comes for free whenever a child was fork()ed.
But this can be defeated, for instance if the parent is a shell and the child was started using nohup child&
, or if the child itself declared that it would ignore HUP signals.
man 2 kill
int kill(pid_t pid, int sig);
If pid is greater than 0, sig shall be sent to the process whose process ID is equal to pid.
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, and for which the process has permission to send a signal.
Unless setpgid
or similar function is called, a child process is in the same process group as its parent. For example, jobs started by your shell belong to the same process group as the shell itself.
Thus kill -HUP $$
sends SIGHUP
to your shell, while kill -HUP -$$
sends SIGHUP
to all processes in your current session, including children of your shell.
This bash script will kill itself and child processes... the opposite of nohup.
#!/bin/bash
read pid cmd state ppid pgrp session tty_nr tpgid rest < /proc/self/stat
trap "kill -TERM -$pgrp; exit" EXIT TERM KILL SIGKILL
# run the program given on args
"$@"
exit $?
Does anyone know if there is a builtin like this?
Yes, it will; use kill -1
: http://unixhelp.ed.ac.uk/shell/jobz5.html
精彩评论