验证拒绝命令

mik*_*ers 11 bash process

我发出^z; bg; disown序列是为了允许我关闭一个 ssh 会话,在该会话中我正在运行一个非常重要的长时间运行的进程。此进程将状态输出写入 stderr,并且即使在分离后它仍会继续这样做(使用 lsof 验证,s​​tderr fd 对 r/w 是开放的)。

有没有办法确定该进程确实已被否认(如果 shell 接收一个,则不会接收 SIGHUP)?

lun*_*chs 13

在 Bash 中,disown自身发出的命令将从活动作业表中删除后台(通过bg&)进程,并将它们标记为在注销时不接收 SIGHUP。

您还可以将一项或多项工作传递给拒绝,例如disown 1 3. disown -h如果您想将作业保留在表中,但在注销时仍然不是 SIGHUP,则该标志很有用。

您可以通过发出jobs命令来查看作业表。后台成功后,会显示[1]+ command &. 取消工作后,它应该不再显示在工作表中,并且不再在注销时被杀死。您仍然可以通过查看的过程中ps uxtop其他工艺观看公用事业和。

取消作业后,您可以等待它自然终止或通过kill向 PID发送信号以停止它。

因为 Bash 只是从要终止的正在运行的作业列表中删除作业,并且终端的 stdout 和 stderr 的文件句柄仍然打开,所以您将继续接收作业的输出,直到您的终端设备关闭(当您注销时) .

例子:

# we start a command in the background
$ cat /dev/urandom > test &
[1] 18533

# we see our command is still running
$ jobs
[1]+  Running                 cat /dev/urandom > test &

# we disown the backgrounded job
$ disown 1

# notice it is no longer in the job table
$ jobs
Run Code Online (Sandbox Code Playgroud)

我通常只disown在我运行一个可能长时间运行的命令时使用,例如rsyncorcp然后决定我需要注销而不终止它。如果您知道要运行命令并注销,您可以通过管道或teeing 将其输出到文件、使用nohup或运行它来捕获输出screen(这允许您重新获得命令的所有权/之后终止)。

例子:

# capture stdout and stderr to separate logs
cat /dev/urandom >stdout.log 2>stderr.log

# capture stdout and stderr to the same log, and display to stdout as well
cat /dev/urandom 2>&1 | tee output.log

# run a command under nohup (doesn't require a disown or job control support)
nohup cat /dev/urandom </dev/null
Run Code Online (Sandbox Code Playgroud)