什么时候停止HUP得到发送,我能做些什么呢? [英] When did HUP stop getting sent and what can I do about it?

查看:173
本文介绍了什么时候停止HUP得到发送,我能做些什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早在我年轻的时候和Unix是新事物,创造一个过程,做的获取,当你退出是一个挑战被杀的那一天。我们使用nohup命令来保护我们的进程中从 HUP 信号。如果我们不小心,我们的流程会被打死的时候,我们注销,甚至关闭我们开始他们的外壳。

Back in the day when I was young and Unix was the new thing, creating a process that did not get killed when you logged out was a challenge. We used the nohup command to protect our persistent processes from the HUP signal. If we weren't careful, our processes would get killed when we logged off, or even closed the shell we started them from.

快进到今天,我觉得我很惊讶的是,默认的似乎是完全相反的。在这两个Ubuntu和Red Hat系统我发现我可以把几乎所有的进程在后台,杀死母贝,注销,任何东西,它只是不断去。我看到的bash脚本,Python脚本和C程序相同的行为。我从xterm或SSH会话相同的行为。

Fast forward to today and I find I am surprised that the default appears to be exactly the opposite. On both Ubuntu and Red Hat systems I find that I can put nearly any process in the background, kill the parent shell, log off, anything and it just keeps going. I see the same behavior with Bash scripts, Python scripts and C programs. I get the same behavior from xterm or ssh sessions.

例如在xterm或SSH窗口中键入:

For example in an xterm or ssh window, type:

while [ 1 ]; do date; sleep 10; done > /tmp/out &

现在从另一个窗口中运行

Now from another window run

tail -f /tmp/out

看着它打印日期每10秒钟,然后关闭用Ctrl-D原始父外壳。仍在运行。注销并回。仍在运行。

Watch it print the date every 10 seconds, then close the original parent shell with Ctrl-D. Still running. Log out and back in. Still running.

发送一个 HUP 信号时,它会立即死亡。

Send it a HUP signal, it instantly dies.

我可以表现出与Python脚本或C程序相同的行为。睡眠与否并不重要。例如,这种丑陋的C程序的行为是一样的:

I can exhibit the same behavior with a Python script or a C program. Sleep or not doesn't matter. For example, this ugly C program behaves the same:

#include <stdio.h>

void main() {
    while(1) {
        printf("*\n");
        fflush(stdout);
        int i, j, k = 0;
        for(i=0; i < 10000; i++) {
            for(j=0; j < 100000; j++) {
                k += i * j;
            }
        }
    }
}

这是完全违背我的青春的方法。我想我只是当它变没注意到?任何历史学家那里知道什么时候会发生?是否 HUP 甚至可以用于这个目的了吗?

This is completely counter to the ways of my youth. I guess I just didn't notice when it changed? Any historians out there know when this happened? Does HUP even get used for this purpose any more?

如果这确实是事情的当前状态,我的问题是:如何安排一个过程,当用户注销或断开连接死

If indeed this is the current state of things, my question is: How can I arrange for a process to die when the user logs off or gets disconnected?

我已经得到了包括留意 PPID (父PID)一劈改变,但肯定有一些东西比这更优雅。

I've got a hack that involves watching for the ppid (parent pid) to change but surely there is something more elegant than that.

推荐答案

我相信你正在寻找的 huponexit shell选项。您可以轻松地设置此

I believe you're looking for the huponexit shell option. You can set this easily with

$ shopt -s huponexit

从bash的手册页的一些细节:

Some details from the bash man page:

通过在收到SIGHUP默认的shell退出。之前
  退出,交互的shell重新发送SIGHUP所有作业,运行
  要么
         停止。已停止作业发送SIGCONT以确保他们收到SIGHUP。以prevent从信号发送到外壳
  一杆
         满足特殊的工作,应该从与不认内置的作业表中删除(见SHELL BUILTIN以下命令)或标注不
  接收
         使用SIGHUP不认-h。

The shell exits by default upon receipt of a SIGHUP. Before exiting, an interactive shell resends the SIGHUP to all jobs, running or stopped. Stopped jobs are sent SIGCONT to ensure that they receive the SIGHUP. To prevent the shell from sending the signal to a par- ticular job, it should be removed from the jobs table with the disown builtin (see SHELL BUILTIN COMMANDS below) or marked to not receive SIGHUP using disown -h.

如果该huponexit外壳选项设置禁用了javascript带,庆典发送SIGHUP所有作业当一个交互式登录shell退出。

If the huponexit shell option has been set with shopt, bash sends a SIGHUP to all jobs when an interactive login shell exits.

这篇关于什么时候停止HUP得到发送,我能做些什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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