shell - 进程被 运行 在后台分离并重定向 STDIN/OUT?

shell - Process detached by running in background and redirecting STDIN/OUT?

在服务器上的 ssh 会话中,我在后台使用 & 启动了一些 bash 进程(启动其他进程)并将它们的标准输入/标准输出重定向到文件(<dev/null >mylog.out ).

进程未在 jobs 下列出,ps -T 仅显示一个 bash 进程和 ps 本身。

大多数(子)进程由用户 root 运行 进行,但都链接到特定的 TTY pts/20

如果我关闭 ssh 会话,进程及其子进程会收到 HUP 信号还是会继续 运行ning?如果是,我怎样才能完全分离它们?

如果您将某个进程置于后台,该进程将忽略 SIGHUP 命令。所以这个过程会一直持续到运行.

测试结果:

在后台登录和 运行 bash 脚本 o.sh 并退出:

system:10.11.2.122 > o.sh &
system:10.11.2.122 > exit 

重新登录系统并检查 o.sh 进程:

system:10.11.2.122 > ps -ef | grep o.sh
root     19115     1  0 10:10 ?        00:00:00 /usr/local/bin/bash /root/scripts/o.sh
root     27774 20197  0 10:23 pts/2    00:00:00 grep o.sh

此方法守护一个 运行ning 进程。

对于 bash,此行为取决于 huponexit shell 选项。这些和其他选项设置可以在 CentOS/RedHat 6.7.

上使用 shopt 命令查看

huponexit 在 CentOS/RedHat 6.7 上似乎默认关闭。

shopt输出的一个子集:

histappend      off
histreedit      off
histverify      off
hostcomplete    on
huponexit       off
interactive_comments    on
lithist         off
login_shell     on
mailwarn        off
no_empty_cmd_completion off
nocaseglob      off
nocasematch     off
nullglob        off

所以回答你的问题:

如果我关闭 ssh 会话,进程及其子进程会收到 HUP 信号还是会继续 运行ning?

这取决于您的 huponexit 设置。如果 huponexitoff,则 并且过程将继续到 运行。否则,.

如果是,我怎样才能完全分离它们?

执行进程时使用nohup命令。

nohup your_process &