Linux nohup 命令

nohup 表示不挂断地运行命令,是 no hangup 的缩写,语法格式如下:

1
nohup Command [ Arg ... ] [ & ]
  • nohup 命令运行由 Command参数和任何相关的 Arg参数指定的命令,忽略所有挂断(SIGHUP)信号。
  • nohup放在命令的开头,表示不挂起(no hang up),也即,关闭终端或者退出某个账号,进程也继续保持运行状态,一般配合&符号一起使用。如 nohup command &。

man 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
NOHUP(1)                                                                                                                                                                  User Commands                                                                                                                                                                 NOHUP(1)

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 /dev/null. 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'.

NOTE: 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.

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

AUTHOR
Written by Jim Meyering.

COPYRIGHT
Copyright © 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://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
The full documentation for nohup is maintained as a Texinfo manual. If the info and nohup programs are properly installed at your site, the command

info coreutils 'nohup invocation'

should give you access to the complete manual.

通过上面 man nohup 可以了解到:

  • 如果不将 nohup 命令的输出重定向,输出将附加到当前目录的 nohup.out 文件中。
  • 如果当前目录的 nohup.out 文件不可写,输出重定向到 $HOME/nohup.out 文件中。

nohup 重定向输出

主要是针对常见的如:2>&1/dev/null 等参数的解释。下表是各字符的含义解释:

描述
/dev/null 表示空设备文件
0 stdin 标准输入
1 stdout 标准输出
2 stderr 标准错误
> file 将标准输出输出到file中,也就相当于 1>file;
2 > error 将错误输出到error文件中
2>&1 将错误重定向到标准输出上
2>&1 >file 将错误输出到终端,标准输出重定向到文件file,等同于 > file 2>&1

有一点需要注意,对于 > file>> file

  • > 表示的是定向输出到文件,如果文件不存在,就创建文件;如果文件存在,就将其清空;一般我们备份清理日志文件的时候,就是这种方法:先备份日志,再用>,将日志文件清空(文件大小变成0字节);
  • >> 表示的是将输出内容追加到目标文件中。如果文件不存在,就创建文件;如果文件存在,则将新的内容追加到那个文件的末尾,该文件中的原有内容不受影响。

& 符号

& 放在命令到结尾,表示后台运行,防止终端一直被某个进程占用,这样终端可以执行别到任务,配合 >file 2>&1 可以将 log 保存到某个文件中,但如果终端关闭,则进程也停止运行。如 command > file.log 2>&1 & 。

使用

  • 在控制台输出日志

    1
    nohup java -jar $GLMAPPER_APP_NAME.jar  &
  • 不输出控制台日志

    1
    nohup java -jar $GLMAPPER_APP_NAME.jar  >/dev/null 2>&1 &

一般情况下,我们的服务都是有配置指定的日志输出路径的,比如 java 中使用 logback 作为日志组件,在 logback-spring.xml 配置文件中指定了日志目录,那么这种情况下,我们就可以不需要将日志输出到控制台,则使用第二种方式就比较合理。

对于有些服务如果启动后将日志输出到控制台,然后默认产生一个 nohup.out 文件,随着服务运行时间变长,nohup.out 文件可能会非常大;如果期望在不停服务的情况下清空 nohup.out 文件,则可以通过以下几种方式来完成

  • cp /dev/null nohup.out
  • cat /dev/null > nohup.out
  • echo “” > nohup.out

最后还是建议配置日志路径,不要使用默认路径将日志输出。

作者

卫恒

发布于

2023-08-15

更新于

2023-08-15

许可协议

评论