nohup:用户退出系统进程继续工作

xiaohai 2021-05-25 19:02:56 1886人围观 标签: Linux 
简介nohup命令可以将程序以忽略挂起信号的方式运行起来,被运行程序的输出信息将不会显示在终端。
说明

  nohup命令可以将程序以忽略挂起信号的方式运行起来,被运行程序的输出信息将不会显示在终端。
  无论是否将hohup命令的输出重定向到终端,输出都会写到当前目录的nohup.out文件中。如果当前目录的hohup.out文件不可写,则输出重定向到$HOME/nohup.out文件中

格式

  nohup [option]

示例

正常情况下,如果用户退出登录或会话终止,则用户正在执行的命令将会自动终止。使用nohup命令可以实现在用户退出或当前会话终止后继续保持运行。如:

[root@localhost ~]# nohup ping www.baidu.com
nohup: ignoring input and appending output to ‘nohup.out’

现在可以关闭终端,但是不要使用ctrl+c来终止,ctrl+c终止后,就不会继续执行了。关闭终端后,重新打开,刚刚的那个进程还在执行

[root@localhost ~]# ps -aux|grep ping
root       6281  0.0  0.1 120744  1104 ?        S    01:44   0:00 ping www.baidu.com
root       6307  0.0  0.0 112660   976 pts/1    S+   01:46   0:00 grep --color=auto ping

如果要使用ctrl+c,那么可以配合&一起使用

[root@localhost ~]# nohup ping www.baidu.com &
[1] 6309
[root@localhost ~]# nohup: ignoring input and appending output to ‘nohup.out’

#这里使用ctrl+c,在查看进程信息
[root@localhost ~]# ps -aux|grep ping
root       6309  0.1  0.1 120744  1108 pts/1    S    01:47   0:00 ping www.baidu.com
root       6311  0.0  0.0 112660   976 pts/1    R+   01:47   0:00 grep --color=auto ping