找回密码
 立即注册
Qt开源社区 门户 查看内容

Linux 中的 nohup 命令介绍

2019-10-5 18:36| 发布者: admin| 查看: 1133| 评论: 0

摘要: 我们经常会碰到这样的问题,用 ssh 登录了远程的 Linux 服务器,运行了一些耗时较长的任务, 结果却由于网络的不稳定导致任务中途失败。如何让命令提交后不受本地关闭终端窗口/网络断开连接的干扰呢?方法有很多,这 ...


我们经常会碰到这样的问题,用 ssh 登录了远程的 Linux 服务器,运行了一些耗时较长的任务, 结果却由于网络的不稳定导致任务中途失败。如何让命令提交后不受本地关闭终端窗口/网络断开连接的干扰呢?

方法有很多,这里先只讲 nohup 这种方式。

nohup 是 Linux 的一个常用命令,当你想要在退出账户或者关闭终端后进程仍在运行时,就可以使用 nohup 命令。nohup 就是不挂断的意思(no hang up)。


hangup 名称的来由

在 Unix 的早期版本中,每个终端都会通过 modem 和系统通讯。当用户 logout 时,modem 就会挂断(hang up)电话。同理,当 modem 断开连接时,就会给终端发送 hangup 信号来通知其关闭所有子进程。


顾名思义,nohup 的用途就是让提交的命令忽略 hangup 信号。让我们先来看一下 nohup 的帮助信息:
    NOHUP(1) User Commands NOHUP(1)


    NAME nohup - run a command immune to hangups, withoutputto a non-tty
    SYNOPSIS nohup COMMAND [ARG]... nohup OPTION
    DESCRIPTION Run COMMAND, ignoring hangup signals.
    --help display this help and exit
    --versionoutputversion information andexit
    If standard inputis a terminal, redirect it from /dev/null. If standard outputis a terminal, append outputto'nohup.out'if possible,'$HOME/nohup.out' otherwise. If standard erroris a terminal, redirect it to standard output. TosaveoutputtoFILE, use'nohup COMMAND > FILE'.
    NOTE: your shell may have its own versionof 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/>...

    可见,nohup 的使用是十分方便的,只需在要处理的命令前加上 nohup 即可,标准输出和标准错误缺省会被重定向到 nohup.out 文件中。一般我们可在结尾加上"&"来将命令同时放入后台运行,也可用">filename 2>&1"来更改缺省的重定向文件名。

    01



    基本使用

      [root@somehost test]# nohup ping www.baidu.com &[1] 14931[root@somehost test]# nohup: ignoring input and appending output to ‘nohup.out’
      [root@somehost test]# ps -ef | grep 14931root 149319265015:57 pts/000:00:00 ping www.baidu.comroot 149549265015:57 pts/000:00:00 grep --color=auto 14931

      可见我们把 ping www.baidu.com 放到后台执行了,由于没有指定重定向,默认情况下标准输出和标准错误缺省会被重定向到当前目录的 nohup.out 文件中:
        [root@somehost test]# tail -f nohup.out PING www.a.shifen.com (14.215.177.39) 56(84) bytes of data.64 bytes from14.215.177.39 (14.215.177.39): icmp_seq=25 ttl=52 time=7.79 ms64 bytes from14.215.177.39 (14.215.177.39): icmp_seq=26 ttl=52 time=7.76 ms...



        02



        重定向标准输出

          [root@somehost test]# nohup ping www.baidu.com > ping.txt &[1] 15559[root@somehost test]# nohup: ignoring input and redirecting stderr to stdout
          [root@somehost test]# tail -f ping.txtPING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.64 bytes from14.215.177.38 (14.215.177.38): icmp_seq=1 ttl=52 time=7.54 ms64 bytes from14.215.177.38 (14.215.177.38): icmp_seq=2 ttl=52 time=7.55 ms...


          03



          重定向标准输出和标准错误

            [root@somehost test]# nohup ping www.baidu.com > ping.txt 2>&1 &[1] 15757[root@somehost test]# tail -f ping.txt nohup: ignoring inputPING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.64 bytes from14.215.177.38 (14.215.177.38): icmp_seq=1 ttl=52 time=7.53 ms64 bytes from14.215.177.38 (14.215.177.38): icmp_seq=2 ttl=52 time=7.52 ms...

            与示例二相比,多了2>&1,下面来解释下这是什么意思。

            04



            扩展

            基本符号的含义

            • 0 表示 stdin 标准输入

            • 1 表示 stdout 标准输出

            • 2 表示 stderr 标准错误


            command > file 命令其实是一个缩写,实际上是 command 产生的标准输出重定向到 file 中,也就是说相当于执行了 command 1 > file。

            2>&1

            2 是标准错误,1是标准输出,&1可以理解为用标准输出的引用。实际就是把标准错误也重定向到file中。

            综上,nohup ping www.baidu.com > ping.txt 2>&1 & 的意思是后台执行ping命令,把标准输出和错误标准错误都重定向ping.txt文件里。




            ———— END ————

            喜欢本文的朋友,欢迎关注公众号 非著名开发者,阅读更多精彩内容




            ----------------------------------------------------------------------------------------------------------------------
            我们尊重原创,也注重分享,文章来源于微信公众号:非著名开发者,建议关注公众号查看原文。如若侵权请联系qter@qter.org。
            ----------------------------------------------------------------------------------------------------------------------

            鲜花

            握手

            雷人

            路过

            鸡蛋
            
            公告
            可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了