Linux常用命令-nohup

命令

nohup

描述

run a command immune to hangups, with output to a non-tty
后台运行命令

用法

1
nohup COMMAND [ARG]...

选项

注意

nohup表示不会挂起任务,&表示后台运行,一般两个命令配合使用,可以在后台运行命令
默认会输出命令结果到当前目录下的nohup.out文件,如果当前目录没有写权限,则会输出到$HOME/nohup.out文件中,注意只有标准输出会保存到文件中,标准错误会显示到当前终端

示例

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
$ nohup ls
nohup: ignoring input and appending output to ‘nohup.out’
$ more nohup.out
a
anaconda-ks.cfg
b
file
nohup.out
# 输出结果自动追加到nohup.out
$ nohup ls
nohup: ignoring input and appending output to ‘nohup.out’
$ more nohup.out
a
anaconda-ks.cfg
b
file
nohup.out
a
anaconda-ks.cfg
b
file
nohup.out
# 标准错误会直接输出到终端
$ nohup aaa
nohup: ignoring input and appending output to ‘nohup.out’
nohup: failed to run command ‘aaa’: No such file or directory
# 可以指定所有输出到文件,注意&>表示覆盖,&>>表示追加
$ nohup aaa &> a.log
$ more a.log
nohup: ignoring input
nohup: failed to run command ‘aaa’: No such file or directory
$ nohup aaa &>> a.log
$ more a.log
nohup: ignoring input
nohup: failed to run command ‘aaa’: No such file or directory
nohup: ignoring input
nohup: failed to run command ‘aaa’: No such file or directory

# 后台运行任务
$ nohup /data/start.sh &>> /data/a.log &