Could last PID variable $! be wrong? - linux

If a script launches a process and immediately after collects the PID with $!, could it get the wrong PID if somewhere else on the system a process is started in the moment between the time the script launches the process and when the script collects the PID with $!?

The $! special variable holds the PID of the last child process launched that the current shell started. It is never modified by actions happening in some other processes elsewhere on the system.
$! can only be wrong if you yourself launch a new child process before collecting the value, because it's overwritten each time. For example, here the PID of "processX" is lost and the PID of "processY" is printed:
processX &
processY &
echo Child process PID: $!

Related

Windows 10, kill child process, can't get correct PID

I use win10 if it's matters, in bash(git)
I have SH script:
start node ./e2e-tests/apimocker_runner.js & pid1=$!
start npm run protractor -- --binaryPath=./build/appInstaller/win-unpacked/ & pid2=$!
sleep 10
kill $pid1
kill $pid2
when i try kill processes by pid i have error:
bash: kill: (6616) - No such process
As i know in $! we have last child process pid, but this don't work.
Cuz then i type in bash:
start bash & pid=$!
echo $pid
$pid === 1000 (for example)
but if i type
echo $$
in created bash by previous command i have another pid
$$ ==== 1200 (for example)
Also I found what if i type start bash this create non-child process, but i want to create child process and wait for them like wait $pid
How i can do this?
A variable set in one bash instance is not visible in another bash instance which is not its child process (and only then if the parent did export variable to expose it to children).

Linux killing process with kill -9 PID

Tried with some examples like ps and ps -ef,after killing a process by using kill-9 PID in Linux,how to verify weather the process is killed or not?
Just run ps aux stat,pid again, and you will see the process with this pid is either a zombie ('Z' in the first column) or dead.
Edit:
Thanks, Mark B, for pointing me about zombies.
Ater the kill, check the PID of the process:
$ pidof PROCESS
You should see no output if the process is gone.
Another similar way:
$ ps aux | grep PROCESS
Notes:
You can kill your own process, but only root user can kill system process or another user process.
After you KILL the process you can get a zombie, you can still see it in the process list but with process STATE Z (wich means Zombie). Zombies cant be killed, they are already dead, so to "kill" zombies you need to kill the zombie's parents. That said, in general you don’t need to get rid of zombie processes unless you have a large amount of them.

killing Parent process along with child process using SIGKILL

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.

How to get right PID of a group of background command and kill it?

Ok, just like in this thread, How to get PID of background process?, I know how to get the PID of background process. However, what I need to do countains more than one operation.
{
sleep 300;
echo "Still running after 5 min, killing process manualy.";
COMMAND COMMAND COMMAND
echo "Shutdown complete"
}&
PID_CHECK_STOP=$!
some stuff...
kill -9 $PID_CHECK_STOP
But it doesn't work. It seems i get either a bad PID or I just can't kill it. I tried to run ps | grep sleep and the pid it gives is always right next to the one i get in PID_CHECK_STOP. Is there a way to make it work? Can i wrap those commands an other way so i can kill them all when i need to?
Thx guys!
kill -9 kills the process before it can do anything else, including signalling its children to exit. Use a gentler signal (kill by itself, which sends a TERM, should be sufficient). You do need to have the process signal its children to exit (if any) explicitly, though, via a trap command.
I'm assuming sleep is a placeholder for the real command. sleep is tricky, however, as it ignores any signals until it returns (i.e., it is non-interruptible). To make your example work, put sleep itself in the background and immediately wait on it. When you kill the "outer" background process, it will interrupt the wait call, which will allow sleep to be killed as well.
{
trap 'kill $(jobs -p)' EXIT
sleep 300 & wait
echo "Still running after 5 min, killing process manualy.";
COMMAND COMMAND COMMAND
echo "Shutdown complete"
}&
PID_CHECK_STOP=$!
some stuff...
kill $PID_CHECK_STOP
UPDATE: COMMAND COMMAND COMMAND includes a command that runs via sudo. To kill that process, kill must also be run via sudo. Keep in mind that doing so will run the external kill program, not the shell built-in (there is little difference between the two; the built-in exists to allow you to kill a process when your process quota has been reached).
You can have another script containing those commands and kill that script. If you are dynamically generating code for the block, just write out a script, execute it and kill when you are done.
The { ... } surrounding the statements starts a new shell, and you get its PID afterwards. sleep and other commands within the block get separate PIDs.
To illustrate, look for your process in ps afux | less - the parent shell process (above the sleep) has the PID you were just given.

does linux kill command terminate child processes?

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

Resources