如何实现SSH断开后 进程仍然在后台运行

来源:互联网 发布:软件项目验收阶段 编辑:程序博客网 时间:2024/06/06 00:33


解决方法
1.nohup命令
功能:不挂断地运行命令,忽略HUP信号。
语法:nohup command &
实例:

  1. [root@DigMouse ~]# nohup ping 51osos.com > /dev/null &
  2. [1] 13683
  3. [root@DigMouse ~]# nohup: ignoring input and redirecting stderr to stdout
  4. [root@DigMouse ~]# ls
  5. Desktop    Downloads  nohup.out
  6. Documents  Music      Public    Videos 
  7. [root@DigMouse ~]# cat nohup.out
  8. PING cd447.gotoip.net (122.225.57.246) 56(84) bytes of data.
  9. 64 bytes from 122.225.57.246: icmp_seq=1 ttl=56 time=48.6 ms
  10. 64 bytes from 122.225.57.246: icmp_seq=2 ttl=56 time=47.8 ms
  11. 64 bytes from 122.225.57.246: icmp_seq=3 ttl=56 time=49.9 ms
  12. 64 bytes from 122.225.57.246: icmp_seq=4 ttl=56 time=49.5 ms
复制代码
  1. [root@DigMouse ~]# ps -ef | grep ping
  2. root     13683 13655  0 09:33 pts/0    00:00:00 ping 51osos.com
  3. root     13687 13655  0 09:36 pts/0    00:00:00 grep ping
  4. [root@DigMouse ~]#
复制代码
关闭此终端,打开另一个终端使用ps命令,仍然可以查看到ping进程。 无论是否将 nohup 命令的输出重定向到终端,输出都将附加到当前目录的 nohup.out 文件中。如果当前目录的 nohup.out 文件不可写,输出重定向到 $HOME/nohup.out 文件中。
#nohup command > command.out 2>&1 &
上面的例子中nohup command输出的内容输出到了command.out文件中,错误内容输出到了标准输出。

原创粉丝点击