nohup 命令
用途:不掛斷地運行命令。如果你正在執行一個job,並且你希望在退出帳戶/關閉終端之后繼續運行,可以使用nohup命令。nohup就是不掛起的意思( no hang up)。
語法:nohup Command [ Arg … ] [ & ]
描述:nohup 命令運行由 Command 參數和任何相關的 Arg 參數指定的命令,忽略所有掛斷(SIGHUP)信號。在注銷后使用 nohup 命令運行后台中的程序。要運行后台中的 nohup 命令,添加 & ( 表示”and”的符號)到命令的尾部。
無論是否將 nohup 命令的輸出重定向到終端,輸出都將附加到當前目錄的 nohup.out 文件中。如果當前目錄的 nohup.out 文件不可寫,輸出重定向到 $HOME/nohup.out 文件中。如果沒有文件能創建或打開以用於追加,那么 Command 參數指定的命令不可調用。如果標准錯誤是一個終端,那么把指定的命令寫給標准錯誤的所有輸出作為標准輸出重定向到相同的文件描述符。
=======測試
[root@rhel7 tmp]# pwd /tmp [root@rhel7 tmp]# ls [root@rhel7 tmp]# nohup ping 127.0.0.1 & [1] 2547 [root@rhel7 tmp]# nohup: ignoring input and appending output to ‘nohup.out’ [root@rhel7 tmp]# ls nohup.out [root@rhel7 tmp]# tail -f nohup.out --關閉當前連接,重新再打開一個ssh連接,使用tail -f nohup.out命令可以看到ping命令一直在執行 64 bytes from 127.0.0.1: icmp_seq=21 ttl=64 time=0.077 ms 64 bytes from 127.0.0.1: icmp_seq=22 ttl=64 time=0.044 ms 64 bytes from 127.0.0.1: icmp_seq=23 ttl=64 time=0.129 ms 64 bytes from 127.0.0.1: icmp_seq=24 ttl=64 time=0.084 ms 64 bytes from 127.0.0.1: icmp_seq=25 ttl=64 time=0.085 ms 64 bytes from 127.0.0.1: icmp_seq=26 ttl=64 time=0.085 ms 64 bytes from 127.0.0.1: icmp_seq=27 ttl=64 time=0.079 ms 64 bytes from 127.0.0.1: icmp_seq=28 ttl=64 time=0.078 ms 64 bytes from 127.0.0.1: icmp_seq=29 ttl=64 time=0.078 ms 64 bytes from 127.0.0.1: icmp_seq=30 ttl=64 time=0.083 ms 64 bytes from 127.0.0.1: icmp_seq=31 ttl=64 time=0.043 ms 64 bytes from 127.0.0.1: icmp_seq=32 ttl=64 time=0.084 ms ^C [root@rhel7 tmp]#
指定輸出到lxjtest.log。如果不指定,則輸出到nohup.out文件
[root@rhel7 tmp]# nohup ping 127.0.0.1 >lxjtest.log & [1] 2641 [root@rhel7 tmp]# nohup: ignoring input and redirecting stderr to stdout [root@rhel7 tmp]# ls lxjtest.log nohup.out [root@rhel7 tmp]# tail -f lxjtest.log PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.039 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.073 ms 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.043 ms 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.043 ms 64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.044 ms 64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.044 ms 64 bytes from 127.0.0.1: icmp_seq=7 ttl=64 time=0.043 ms 64 bytes from 127.0.0.1: icmp_seq=8 ttl=64 time=0.079 ms 64 bytes from 127.0.0.1: icmp_seq=9 ttl=64 time=0.079 ms 64 bytes from 127.0.0.1: icmp_seq=10 ttl=64 time=0.086 ms ^C [root@rhel7 tmp]#
In earlier versions of the bash shell, background processes were also killed when the shell they were started from was terminated. To prevent that, the process could be started with the nohup command in front of it. Using nohup for this purpose is no longer needed in RHEL 7. (RHEL7版本可以不使用nohup命令)
[root@rhel7 tmp]# ping 192.168.1.111 > lxjtest2.log & [1] 2649 [root@rhel7 tmp]# jobs [1]+ Running ping 192.168.1.111 > lxjtest2.log & [root@rhel7 tmp]#