How can I kill a parent process only?

I have the following processes.

ParentProcess
-- ChildProcess
-- ChildProcess
-- ChildProcess

How can I kill the parent process only? I want the children processes not to be killed. If I try to kill the parent process, the children will get SIGHUP and will get killed.


Generally speaking, when a process becomes orphan (that is, its parent dies) it is adopted by init.

The special situation you describe probably applies to an interactive process when its controlling terminal closes (from Wikipedia):

The SIGHUP signal is sent to a process when its controlling terminal is closed. It was originally designed to notify the process of a serial line drop. In modern systems, this signal usually means that controlling pseudo or virtual terminal has been closed.

To prevent this, child processes should block SIGHUP, so in most cases you need cooperation from the parent process.

If the parent process is a shell (bash, csh and the like) and you want commands you run not to terminate when bash finishes, you can prefix any command with nohup (from info coreutils "nohup invocation"):

'nohup' runs the given COMMAND with hangup signals ignored, so that the command can continue running in the background after you log out.

In this example:

$ tty
/dev/ttys000
$ nohup find /dir -name file -exec rm {} \;

find won't be killed when the shell terminates and closes the controlling terminal /dev/ttys000.

If a shell script should block SIGHUP, use the builtin trap, as explained here for bash.