nohup命令与例子

时间:2020-03-05 15:24:47  来源:igfitidea点击:

Nohup代表'否挂断',并且名称指示在注销或者退出shell时会阻止命令自动中止。

当我们执行Linux命令并在从会话中退出某个时间后注销该命令未完成执行时,此过程将被杀死。

有各种需要数小时的程序才能完成。
我们不需要等待命令完成整个任务。
我们可以使用NOHUP命令保留在后台运行的程序并稍后检查输出。

nohup命令语法

nohup命令的语法是:

nohup COMMAND [ARG]...

默认情况下,命令的输出显示在标准输出设备上,该输出设备是启动该过程的终端。
但是,由于终端可以在子过程的寿命期间关闭,因此终端不能再是输出设备。
根据Nohup命令的信息页面,

If standard output is a terminal, the command's standard output is appended to the file `nohup.out'; if that cannot be written to, it is appended to the file `$HOME/nohup.out'; and if that cannot be written to, the command is not run. Any `nohup.out' or `$HOME/nohup.out' file created by `nohup' is made readable and writable only to the user, regardless of the current umask settings.

同样用于标准错误,

If standard error is a terminal, it is normally redirected to the same file descriptor as the (possibly-redirected) standard output. However, if standard output is closed, standard error terminal output is instead appended to the file `nohup.out' or `$HOME/nohup.out' as above.

这意味着默认情况下,标准输出和标准错误都被重定向到"nohup.out"文件。
但如果我们希望将输出重定向到其他文件,则可以通过使用重定向运算符来始终执行此操作。
例如,捕获制作的输出,

nohup make > make.log

对于标准输入,

If standard input is a terminal, it is redirected from `/dev/null' so that terminal sessions do not mistakenly consider the terminal to be used by the command. This is a GNU extension; programs intended to be portable to non-GNU hosts should use `nohup COMMAND [ARG]...

nohup命令不会自动放入背景中。
这必须明确与&。

nohup make > make.log &

此外,此命令不会更改命令的nicity(即优先级)。
为此,尼斯命令用作"Nohup nice命令"。

如何使用nohup命令

我们可以通过两种方式使用NOHUP。
首先,使用参数运行命令,而无需在工作时接收任何其他输入。
所有输出,包括任何错误消息,都将写入工作目录中的文件nohup.out,或者在主目录中写入。
如果在注销或者关闭终端时仍在运行该命令,则不会终止。

让我们尝试一个拍照的例子。

$nohup sudo memtester 2048 5
nohup: ignoring input and appending output to 'nohup.out'
Killed
$cat nohup.out 
memtester version 4.3.0 (64-bit)
Copyright (C) 2001-2012 Charles Cazabon.
Licensed under the GNU General Public License version 2 (only).
pagesize is 4096
pagesizemask is 0xfffffffffffff000
want 2048MB (2147483648 bytes)
got  1876MB (1968074752 bytes), trying mlock ...

我们还可以使用>符号将输出重定向到其他文件而不是nohup.out。

$nohup sudo memtester 2048 5 > result.txt

使用"&"使用nohup命令

要启动一个进程并将其放在后台中,我们基本上执行命令后跟"&"符号。
命令末尾的"&"符号指示bash在后台运行"nohup yourcommand`。
它可以用FG命令返回前景。

$nohup bash sleep1.sh &
[1] 653
$nohup: ignoring input and appending output to 'nohup.out'
fg
-bash: fg: job has terminated
[1]+  Exit 127                nohup bash sleep1.sh

在后台运行多个命令

我们可以使用nohup命令在后台运行多个命令。
在以下示例中,使用NOHUP和BASH命令在后台执行MKDIR,触摸和LS命令。

$nohup bash -c 'mkdir TestDir && touch test2.file && ls'> output.txt
nohup: ignoring input and redirecting stderr to stdout
$cat output.txt 
TestDir
nohup.out
output.txt
test2.file
testfile.txt

在后台中启动和结束过程

如果使用NOHUP运行ping命令,则关闭终端时的进程将不会终止。

$nohup ping -i 15 theitroad.com &
[1] 806
$nohup: ignoring input and appending output to 'nohup.out'

我们可以关闭终端并重新打开它。
现在使用"-a""选项运行pgrep命令。

$pgrep -a ping
816 ping -i 15 theitroad.com

我们将获得正在运行的进程ID的进程列表。
正如我们所看到的,我们离开运行的ping命令未被终止。
要终止任何后台进程,我们必须运行kill命令。
作为参数,我们必须使用正在运行的特定进程ID。

在我们的情况下,进程ID为816.运行杀死816终止该过程。

$pgrep -a ping
816 ping -i 15 theitroad.com
$kill 816