简体   繁体   English

如何向从 shell 脚本启动的所有进程发送 SIGSTOP

[英]How to send SIGSTOP to all processes that launched from a shell script

I have a shell script that launches 4 other binaries.我有一个启动其他 4 个二进制文件的 shell 脚本。 I am sending SIGSTOP to the shell script.我将 SIGSTOP 发送到 shell 脚本。 Does this stop all other 4 processes also?这是否也会停止所有其他 4 个进程? If not, what should I do to forward the SIGSTOP to these processes?如果没有,我应该怎么做才能将 SIGSTOP 转发到这些进程? Similar is the case with SIGCONT. SIGCONT 的情况类似。

I have the C source code for all the 4 binaries.我有所有 4 个二进制文件的 C 源代码。

You can call setpgid() in the forked child process that will execute the shell script.您可以在将执行 shell 脚本的分叉子进程中调用setpgid() That will give any spawned processes from that shell script the same group ID as the child process.这将为从该 shell 脚本生成的任何进程提供与子进程相同的组 ID。 You can then use killpg() to send a signal to the entire group that all processes in that group will receive.然后,您可以使用killpg()向整个组发送一个信号,该组中的所有进程都将收到该信号。

For instance, if inside the child process you called setpgid(0, 0) , this would setup a special instance where the child-process' group ID will be set to the same value as the child's PID.例如,如果在您调用setpgid(0, 0)的子进程内部,这将设置一个特殊实例,其中子进程的组 ID 将设置为与子进程 PID 相同的值。 Then any processes overlaid on the child process using one of the exec family of functions will have the same group-ID value that the child had.然后,使用exec系列函数之一覆盖在子进程上的任何进程都将具有与子进程相同的组 ID 值。 In addition, any processes that the newly overlaid process may spawn will also have the same group ID (ie, your shell-script).此外,新覆盖的进程可能产生的任何进程也将具有相同的组 ID(即,您的 shell 脚本)。 You can then, using killpg() , send a signal to any processes sharing a group ID value using just the child's PID value that fork() returned since the group ID of the child process is the same value as the child's PID after the setpgid(0, 0) call.然后,您可以使用killpg()仅使用fork()返回的子进程的 PID 值向共享组 ID 值的任何进程发送信号,因为子进程的组 ID 与setpgid(0, 0)之后的子进程的 PID 值相同setpgid(0, 0)调用。

If you are using fork() , depending on how quickly you need to send signals to the group from the parent process may create some synchronization issues... for example, you want to immediately send a signal to the process group right after forking the child process.如果您使用fork() ,取决于您需要多快从父进程向组发送信号可能会产生一些同步问题......例如,您希望在分叉后立即向进程组发送信号子进程。 There are two work-arounds for this: Either 1) use vfork() instead of fork, so that the parent is suspended until the child has changed it's group-ID and successfully called exec , or 2) call setpgid() in the parent process as well as in the child-process, but in the parent, rather than using setgpid(0, 0) like you would in the child, you can use setpgid(CHILD_PID, CHILD_PID) .有两种解决方法:1)使用vfork()而不是 fork,以便父级暂停,直到子级更改其组 ID 并成功调用exec ,或 2)在父级中调用setpgid()进程和子进程一样,但是在父进程中,您可以使用setpgid(CHILD_PID, CHILD_PID) ,而不是像在子进程中那样使用setgpid(0, 0) ) 。 Then it won't matter which call was successful (one of them will be successful, and the other will fail with a EACCES ), and any successive signals sent from the parent wil now go to a valid group ID.然后,哪个调用成功(其中一个成功,另一个失败并带有EACCES )都无关紧要,并且从父级发送的任何连续信号现在都会 go 到有效的组 ID。

If your processes form a group, you can use standard kill (1).如果您的进程形成一个组,则可以使用标准kill (1)。 man kill has the following info: man kill有以下信息:

pid... 
    Specify the list of processes that kill should signal. Each pid can be one of five things: 
n 
    where n is larger than 0. The process with pid n will be signaled. 
    All processes in the current process group are signaled.
-1 
    All processes with pid larger than 1 will be signaled. 
-n 
     where n is larger than 1. All processes in process group n are signaled. When an argument of the form '-n' is given, and it is meant to denote a process group, either the signal must be specified first, or the argument must be preceded by a '--' option, otherwise it will be taken as the signal to send. 
commandname

It seems to me that the '-n' specification might help you在我看来,“-n”规范可能会对您有所帮助

kill -STOP -- "-$(pgrep myparentproc)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM