bash使用陷阱SIGCHLD重新启动子过程? [英] bash restart sub-process using trap SIGCHLD?

查看:129
本文介绍了bash使用陷阱SIGCHLD重新启动子过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到监视程序,这些脚本要么定期使用"ps"或服务状态(在Linux上)"检查进程状态的脚本,要么在C/C ++中分叉并等待进程...

I've seen monitoring programs either in scripts that check process status using 'ps' or 'service status(on Linux)' periodically, or in C/C++ that forks and wait on the process...

我想知道是否可以将bash与trap一起使用并在收到SIGCLD后重新启动子进程?

I wonder if it is possible to use bash with trap and restart the sub-process when SIGCLD received?

我已经按照以下想法在RedHat Linux上测试了一个基本套件(当然它是行不通的...)

I have tested a basic suite on RedHat Linux with following idea (and certainly it didn't work...)

#!/bin/bash
set -o monitor # can someone explain this? discussion on Internet say this is needed
trap startProcess SIGCHLD
startProcess() { 
  /path/to/another/bash/script.sh & # the one to restart
  while [ 1 ]
  do
    sleep 60
  done
}
startProcess

正在启动的bash脚本只是睡了几秒钟然后暂时退出.

what the bash script being started just sleep for a few seconds and exit for now.

观察到几个问题:

  • 当Shell从前台启动时,SIGCHLD将仅被处理一次.是否捕获像signal()这样的复位信号处理?
  • 该脚本及其子级似乎不受SIGINT的影响,这意味着它们不能被^ C阻止
  • 由于无法关闭,我关闭了终端.该脚本似乎是HUP,剩下许多僵尸孩子.
  • 在后台运行时,脚本导致终端死机

...无论如何,这根本不起作用.我不得不说我对这个话题知之甚少. 有人可以提出建议或给出一些可行的例子吗? 有用于这种用途的脚本吗?

... anyway, this does not work at all. I have to say I know too little about this topic. Can someone suggest or give some working examples? Are there scripts for such use?

那么在bash中使用wait怎么样?

how about use wait in bash, then?

谢谢

推荐答案

我可以尝试回答您的一些问题,但并非全部基于我的回答 知道.

I can try to answer some of your questions but not all based on what I know.

  1. set -o monitor行(或等效地,set -m)打开作业 控件,仅在交互式shell的默认情况下处于启用状态.好像 发送SIGCHLD所需的信息.但是,工作控制更像是 交互功能,并不是真的要在shell脚本中使用 (另请参见这个问题).

  1. The line set -o monitor (or equivalently, set -m) turns on job control, which is only on by default for interactive shells. This seems to be required for SIGCHLD to be sent. However, job control is more of an interactive feature and not really meant to be used in shell scripts (see also this question).

还请记住,这可能不是您打算要做的 因为一旦启用作业控制,SIGCHLD就会发送给每个 存在的外部命令(例如,每次您运行lsgrep或 任何事情,当该命令完成并且您的陷阱时,SIGCHLD将触发 将运行).

Also keep in mind this is probably not what you intended to do because once you enable job control, SIGCHLD will be sent for every external command that exists (e.g. every time you run ls or grep or anything, a SIGCHLD will fire when that command completes and your trap will run).

我怀疑SIGCHLD陷阱似乎只运行一次的原因是 因为您的陷阱处理程序包含前台无限循环,所以您 脚本卡在陷阱处理程序中.似乎没有一点 无论如何,您都可以将其删除.

I suspect the reason the SIGCHLD trap only appears to run once is because your trap handler contains a foreground infinite loop, so your script gets stuck in the trap handler. There doesn't seem to be a point to that loop anyways, so you could simply remove it.

脚本对SIGINT的免疫"似乎是启用 作业控制(监视部分).我的直觉是打开工作控制, 运行脚本的bash子实例不再终止 本身是对SIGINT的响应,而是将SIGINT传递给 它的前台子进程.在您的脚本中,^C即SIGINT 就像其他编程语言中的continue语句一样 情况下,由于SIGINT只会杀死当前正在运行的sleep 60, 然后while循环将立即运行新的sleep 60.

The script's "immunity" to SIGINT seems to be an effect of enabling job control (the monitor part). My hunch is with job control turned on, the sub-instance of bash that runs your script no longer terminates itself in response to a SIGINT but instead passes the SIGINT through to its foreground child process. In your script, the ^C i.e. SIGINT simply acts like a continue statement in other programming languages case, since SIGINT will just kill the currently running sleep 60, whereupon the while loop will immediately run a new sleep 60.

当我尝试运行您的脚本然后将其杀死(从另一个脚本中删除) 终端),最后我只有两个杂散的睡眠过程.

When I tried running your script and then killing it (from another terminal), all I ended up with were two stray sleep processes.

将该脚本作为背景也可以杀死我的外壳,尽管 行为并不十分一致(有时会发生) 立即,其他时间完全不行).似乎键入其他任何键 而不是输入会导致以某种方式发送EOF.即使在终端之后 退出脚本将继续在后台运行.我不知道 这是怎么回事.

Backgrounding that script also kills my shell for me, although the behavior is not terribly consistent (sometimes it happens immediately, other times not at all). It seems typing any keys other than enter causes an EOF to get sent somehow. Even after the terminal exits the script continues to run in the background. I have no idea what is going on here.

更具体地说明您要完成的工作会有所帮助.如果 您只希望命令在您的整个生命周期中连续运行 脚本,您可以在后台运行无限循环,例如

Being more specific about what you want to accomplish would help. If you just want a command to run continuously for the lifetime of your script, you could run an infinite loop in the background, like

while true; do
    some-command
    echo some-command finished
    echo restarting some-command ...
done &

注意done之后的&.

对于其他任务,wait可能是比使用作业控制更好的主意 在shell脚本中.同样,这取决于您到底要尝试什么 要做.

For other tasks, wait is probably a better idea than using job control in a shell script. Again, it would depend on what exactly you are trying to do.

这篇关于bash使用陷阱SIGCHLD重新启动子过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆