What's the best way to send a signal to all members of a process group?
You don't say if the tree you want to kill is a single process group. (This is often the case if the tree is the result of forking from a server start or a shell command line.) You can discover process groups using GNU ps as follows:
ps x -o "%p %r %y %x %c "
If it is a process group you want to kill, just use the kill(1)
command but instead of giving it a process number, give it the negation of the group number. For example to kill every process in group 5112, use kill -TERM -- -5112
.
What's the best way to send a signal to all members of a process group?
You don't say if the tree you want to kill is a single process group. (This is often the case if the tree is the result of forking from a server start or a shell command line.) You can discover process groups using GNU ps as follows:
ps x -o "%p %r %y %x %c "
If it is a process group you want to kill, just use the kill(1)
command but instead of giving it a process number, give it the negation of the group number. For example to kill every process in group 5112, use kill -TERM -- -5112
.
Signal all processes in process group except self
You cannot send a signal to all members of the group except the sender, but there is a reasonable workaround. Rather than using SIGSTOP
(which cannot be handled or ignored), you can use SIGTSTP
which by default will have the same effect as SIGSTOP
, stopping the process which receives it. To avoid stopping the sender, simply have the sender ignore the signal before it is sent, and then reset the signal disposition to the default after sending signal to the process group.
What order are signals sent to a process group in Linux?
I've been browsing the Linux kernel source and I think I have the answer. When sending a signal via kill, Linux iterates over the linked list held in the pid structure, which contains all the tasks that use that PID. I believe that this means it iterates over processes in a group in the reverse order they were forked() as the PID is added to the linked list with hlist_add_head_rcu, which inserts it at the head. I don't think this behavior should be relied on though, as they could change it in several ways.
can a parent send signal to all child processes in the same exact time?
TL;DR — No
The POSIX specification for kill()
gives several ways of sending a signal to multiple processes:
If pid is 0, sig shall be sent to all processes (excluding an unspecified set of system processes) whose process group ID is equal to the process group ID of the sender, and for which the process has permission to send a signal.
If pid is -1, sig shall be sent to all processes (excluding an unspecified set of system processes) for which the process has permission to send that signal.
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.
Your request is for 'all children'. That isn't feasible if any of the children have changed the process group ID — and that is something they're at liberty to do. Also, if any of the children have since executed a SUID program, you may well have lost permission to send them signals.
The pid value of -1
is quite dangerous; I believe it would go to all processes with the same (effective) UID as the current process.
How can kill all the processes triggered by hotkey with some smart way?
From the extended context you provided the first thing that comes to my mind is using a PID-file and a companion command.
Something like this:
# cr.sh
ffmpeg ... &
# note down pid
echo $! >/tmp/cr.pid
# cr-stop.sh
kill $(cat /tmp/cr.pid)
then you should be able to use cr-stop.sh
to stop ffmpeg.
Related Topics
How to Prompt For Yes/No/Cancel Input in a Linux Shell Script
What Happens to an Open File Handle on Linux If the Pointed File Gets Moved or Deleted
How to Recursively Grep All Directories and Subdirectories
Why Do X86-64 Linux System Calls Modify Rcx, and What Does the Value Mean
How to Find Out Line-Endings in a Text File
How to Replace a String in Multiple Files in Linux Command Line
Apache Virtualhost 403 Forbidden
Linux Flock, How to "Just" Lock a File
Execute Command After Every Command in Bash
Round a Divided Number in Bash
How to Compare Two Strings in Dot Separated Version Format in Bash
What Is the Meaning of "Posix"
Peak Memory Usage of a Linux/Unix Process
How to Use Local Docker Images With Minikube
Redirect Stderr/Stdout of a Process After It's Been Started, Using Command Line