lun*_*chs 13
在 Bash 中,disown自身发出的命令将从活动作业表中删除后台(通过bg或&)进程,并将它们标记为在注销时不接收 SIGHUP。
您还可以将一项或多项工作传递给拒绝,例如disown 1 3. disown -h如果您想将作业保留在表中,但在注销时仍然不是 SIGHUP,则该标志很有用。
您可以通过发出jobs命令来查看作业表。后台成功后,会显示[1]+ command &. 取消工作后,它应该不再显示在工作表中,并且不再在注销时被杀死。您仍然可以通过查看的过程中ps ux,top其他工艺观看公用事业和。
取消作业后,您可以等待它自然终止或通过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)