How can I kill parent process script from child process script, without terminating child process in Linux.
If your shell defines PPID, kill $PPID will kill the parent. If your shell does not define PPID, you can probably get it with PPID=$( ps -o ppid= $$ ). There is no reason for this action to kill the child.
Related
I am running a script like
myparent ../file&
it generates another process, let say, child, how I can grep this PID of child.
my script is like,
myparent ../file&
set PD=$!
echo "PD"
but it gives the PID of myparent command, not child one
I am writing one shell script in which I have parent process and it has child processes which are created by sleep & command. Now I wish to kill the parent process so that the child process will be also killed. I was able to do that this with below command:
trap "kill $$" SIGINT
trap 'kill -HUP 0' EXIT
trap 'kill $(jobs -p)' EXIT
These commands are working with kill [parent_process_ID] commands but if I use kill -9 [parent_process_ID] then only the parent process will be killed.
Please guide me further to achieve this functionality so that when I kill parent process with any command then child process should be also killed.
When you kill a process alone, it will not kill the children.
You have to send the signal to the process group if you want all processes for a given group to receive the signal.
kill -9 -parentpid
Otherwise, orphans will be linked to init.
Child can ask kernel to deliver SIGHUP (or other signal) when parent dies by specifying option PR_SET_PDEATHSIG in prctl() syscall like this:
prctl(PR_SET_PDEATHSIG, SIGHUP);
See man 2 prctl for details.
Sending the -9 signal (SIGKILL) to a program gives no chance for it to execute its own signal handlers (e.g., your trap statements). That is why the children don't get killed automatically. (In general, -9 gives no chance for the app to clean up after itself.) You have to use a weaker signal to kill it (such as SIGTERM.)
See man 7 signal for details.
I run a shell script inside php (using shell_exec). I want to kill it and all processes it created when it is in operation. I know the shell script pid. Do you have any suggestion?
I am using ubuntu, php as apache module.
Thank you.
Example:
#!/bin/bash
echo hello
sleep 20
When I kill my script (shell_exec("sudo kill -9 $pid")), the sleep process is not killed which is not desired.
use
pkill -TERM -P pid
will kill the child processes
see this answer
Use this kill command instead:
kill -- -$pid
to kill the running script and all its spawned children.
The command kill $$ should kill current bash, but it seems that it doesn't work:
$ ps -p $$
PID TTY TIME CMD
18179 pts/4 00:00:00 bash
$ kill $$
$ ps -p $$
PID TTY TIME CMD
18179 pts/4 00:00:00 bash
Why?
I'm not sure why one would like to kill the current shell. Nevertheless...
kill PID would send SIGTERM when no signal is specified. bash ignores SIGTERM and SIGQUIT in the absence of any traps.
You'll achieve the desired effect if you were to say
kill -9 $$
or
kill -SIGKILL $$
Quoting from the manual:
When Bash is interactive, in the absence of any traps, it ignores
SIGTERM (so that ‘kill 0’ does not kill an interactive shell), and
SIGINT is caught and handled (so that the wait builtin is
interruptible). When Bash receives a SIGINT, it breaks out of any
executing loops. In all cases, Bash ignores SIGQUIT.
When you send a process any signal using kill command, the process can choose to handle the signal as per its wish. (There are a few signals, which cannot be handled.)
When you use kill $$, you are actually passing it signal=15 (SIGTERM). It can be handled by a process.
You can google for linux signal example to know HOW it is implemented.
& To answer YOUR question:
If you pass an un-handlable signal, like SIGKILL(9) or SIGSTOP(19), it will respectively kill/stop a running bash process.
When you invoke "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