回到手册索引

命令用途

nohup(no hang up)命令用于在用户退出终端或断开连接时,让进程继续在后台运行,避免被系统挂断信号(SIGHUP)终止。

常用用法示例

  1. 基本用法:运行命令并忽略挂断信号

    1
    2
    3
    nohup sleep 120 &
    [1] 12345
    nohup: ignoring input and appending output to 'nohup.out'

    命令会在后台运行 sleep 120,输出默认追加到 nohup.out 文件。

  2. 指定输出文件

    1
    2
    3
    nohup ping example.com > ping.log &
    [1] 12346
    nohup: ignoring input and redirecting stderr to stdout

    输出内容重定向到 ping.log,错误输出合并到标准输出。

  3. 后台运行并关闭输出

    1
    2
    nohup command > /dev/null 2>&1 &
    [1] 12347

    完全关闭标准输出和错误输出(适用于无需日志的场景)。

  4. 运行脚本并保留输出

    1
    2
    3
    nohup ./script.sh &
    [1] 12348
    nohup: appending output to 'nohup.out'

    脚本的输出会追加到默认的 nohup.out 文件。

  5. 结合 tail 实时查看输出

    1
    2
    3
    nohup command > output.log & tail -f output.log
    [1] 12349
    (实时输出内容)

    启动命令后通过 tail -f 实时监控输出文件。

  6. 忽略输入并运行

    1
    2
    nohup command < /dev/null &
    [1] 12350

    强制忽略标准输入,避免进程因输入等待而挂起。

  7. 追加模式运行多命令

    1
    2
    nohup sh -c 'command1; command2' >> combined.log 2>&1 &
    [1] 12351

    通过 sh -c 执行多个命令,输出追加到日志文件。

  8. 查看 nohup 版本信息

    1
    2
    3
    nohup --version
    nohup (GNU coreutils) 8.32
    ...(版本详情)

    显示当前系统 nohup 的版本信息。

常用参数选项

  • –help
    显示帮助信息并退出。
  • –version
    显示版本信息并退出。
  • 无参数默认行为
    自动忽略挂断信号,输出重定向到 nohup.out(若不可写则保存到 $HOME/nohup.out)。
  • 标准输入重定向
    若输入为终端,默认重定向到 /dev/null(忽略输入)。
  • 标准错误合并
    若标准错误是终端,自动合并到标准输出流。
  • 输出追加模式
    默认以追加模式写入输出文件(不会覆盖原有内容)。
  • 后台运行依赖 &
    需配合 & 实现后台运行(非 nohup 参数,但为常见组合)。
  • 自定义输出路径
    通过 > 或 >> 重定向输出(非 nohup 参数,但为关键用法)。

原厂文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
NAME

nohup - run a command immune to hangups, with output to a non-tty

SYNOPSIS

nohup COMMAND [ARG]...
nohup OPTION

DESCRIPTION

Run COMMAND, ignoring hangup signals.

--help display this help and exit

--version
output version information and exit

If standard input is a terminal, redirect it from an unreadable
file. If standard output is a terminal, append output to
'nohup.out' if possible, '$HOME/nohup.out' otherwise. If standard
error is a terminal, redirect it to standard output. To save
output to FILE, use 'nohup COMMAND > FILE'.

Your shell may have its own version of nohup, which usually
supersedes the version described here. Please refer to your
shell's documentation for details about the options it supports.

Exit status:
125 if the nohup command itself fails

126 if COMMAND is found but cannot be invoked

127 if COMMAND cannot be found

- the exit status of COMMAND otherwise

AUTHOR

Written by Jim Meyering.

REPORTING BUGS

GNU coreutils online help:
<https://www.gnu.org/software/coreutils/>
Report any translation bugs to
<https://translationproject.org/team/>

COPYRIGHT

Copyright © 2025 Free Software Foundation, Inc. License GPLv3+:
GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

SEE ALSO

Full documentation <https://www.gnu.org/software/coreutils/nohup>
or available locally via: info '(coreutils) nohup invocation'

COLOPHON

This page is part of the coreutils (basic file, shell and text
manipulation utilities) project. Information about the project
can be found at ⟨http://www.gnu.org/software/coreutils/⟩. If you
have a bug report for this manual page, see
⟨http://www.gnu.org/software/coreutils/⟩. This page was obtained
from the tarball coreutils-9.6.tar.xz fetched from
⟨http://ftp.gnu.org/gnu/coreutils/⟩ on 2024-02-02. If you
discover any rendering problems in this HTML version of the page,
or you believe there is a better or more up-to-date source for the
page, or you have corrections or improvements to the information
in this COLOPHON (which is not part of the original manual page),
send a mail to man-pages@man7.org