跳转到内容

User:dukeAnt/Fg (Unix)

维基百科,自由的百科全书

nohup 是一个POSIX命令,用于忽略HUP(挂断)信号。

如果没有指定输出文件,正常情况下输出到终端的输出都被重定向到一个名为nohup.out的文件中。

用法

[编辑]

下方的第一个命令,用来在后台启动程序abcd,而随后的登出操作不会终止程序的执行。

$ nohup abcd &
$ exit

Note that these methods prevent the process from being sent a 'stop' signal on logout, but if input/output is being received for these standard I/O files (stdin, stdout, or stderr), they will still hang the terminal.[1] See Overcoming hanging, below.

nohup is often used in combination with the nice command to run processes on a lower priority.

$ nohup nice abcd &

Existing jobs, processes

[编辑]

Some shells (e.g. bash) provide a shell builtin that may be used to prevent SIGHUP being sent or propagated to existing jobs, even if they were not started with nohup. In bash, this can be obtained by using disown -h job; using the same builtin without arguments removes the job from the job table, which also implies that the job will not receive the signal. Before using disown on an active job, it should be stopped by Ctrl-Z, and continued in the background by the bg command.[2] Another relevant bash option is shopt huponexit, which automatically sends the HUP signal to jobs when the shell is exiting normally.[3]

The AIX and Solaris versions of nohup have a -p option that modifies a running process to ignore future SIGHUP signals. Unlike the above-described disown builtin of bash, nohup -p accepts process IDs.[4][5]

Overcoming hanging

[编辑]

Note that nohupping backgrounded jobs is typically used to avoid terminating them when logging off from a remote SSH session. A different issue that often arises in this situation is that ssh is refusing to log off ("hangs"), since it refuses to lose any data from/to the background job(s).[6][7] This problem can also be overcome by redirecting all three I/O streams:

$ nohup ./myprogram > foo.out 2> foo.err < /dev/null &

Also note that a closing SSH session does not always send a HUP signal to depending processes. Among others, this depends on whether a pseudo-terminal was allocated or not.[8]

Alternatives

[编辑]
  • The screen or tmux command can also detach a process from the current shell. It allows one to reattach to the process later on.
e.g. the following call will run somescript.sh detached from SIGHUP and in the background:
 $ screen -A -m -d -S somename ./somescript.sh &
  • The disown command is used to remove jobs from the job table, or to mark jobs so that a SIGHUP signal is not sent

References

[编辑]
[编辑]