nohup cmd &
nohup 的用途就是让提交的命令忽略 hangup 信号。
& 用途:在后台运行
nohup 的使用是十分方便的,只需在要处理的命令前加上 nohup 即可,标准输出和标准错误缺省会被重定向到 nohup.out 文件中。一般我们可在结尾加上"&"来将命令同时放入后台运行,也可用">filename 2>&1"来更改缺省的重定向文件名。
test .c
#include <stdio.h> #include <unistd.h> int main() { int number = 8; printf("begin\n"); for (int i = 0; i < number; i++) { sleep(2); printf("index:%d\n", i); } printf("end\n"); return 0; }
cts@cts-pc:~/test$ gcc test.c
cts@cts-pc:~/test$ ls
a.out test.c
cts@cts-pc:~/test$ ./a.out
begin
index:0
index:1
index:2
index:3
index:4
index:5
index:6
index:7
end
cts@cts-pc:~/test$ ./a.out &
[1] 14782
cts@cts-pc:~/test$ begin
index:0
index:1
//Enter
cts@cts-pc:~/test$ index:2
index:3
index:4
index:5
index:6
index:7
end
//Enter
[1]+ Done ./a.out
cts@cts-pc:~/test$
cts@cts-pc:~/test$
cts@cts-pc:~/test$ ./a.out &
[1] 14783
cts@cts-pc:~/test$ begin
index:0
index:1
//Enter
cts@cts-pc:~/test$ index:2
index:3
ls
a.out test.c
cts@cts-pc:~/test$ index:4
index:5
index:6
index:7
end
//Enter
[1]+ Done ./a.out
cts@cts-pc:~/test$
cts@cts-pc:~/test$ nohup ./a.out //block
nohup: ignoring input and appending output to 'nohup.out'
cts@cts-pc:~/test$ ls
a.out nohup.out test.c
cts@cts-pc:~/test$ cat nohup.out
begin
index:0
index:1
index:2
index:3
index:4
index:5
index:6
index:7
end
cts@cts-pc:~/test$ nohup ./a.out &
[1] 14808
cts@cts-pc:~/test$ nohup: ignoring input and appending output to 'nohup.out'
cts@cts-pc:~/test$ jobs -l
[1]+ 14808 Running nohup ./a.out &
cts@cts-pc:~/test$ jobs -l
[1]+ 14808 Done nohup ./a.out

浙公网安备 33010602011771号