Linux 中的 Nohup 命令

当你离开Linux系统的壳时,所有运行过程通常会被终止或关闭,所以你要做什么如果你仍然想要保持流程的运行,即使离开壳/终端?这就是nohup命令出现的地方。

Nohup指挥

Nohup是Linux系统中的一个命令,即使在离开壳或终端后也保持流程运行。Nohup阻止流程或工作接收SIGHUP(信号挂起)信号。这是在关闭或离开终端时发送给进程的信号。

Nohup 命令语法

Nohup命令语法如下:

1nohup command arguments

1nohup options

让我们看看命令如何进入游戏

查看 Nohup 的版本

您可以通过使用下面的语法检查Nohup的版本开始。

1nohup --version

Output check version of nohup command

使用 Nohup 启动一个过程

如果你想保持你的流程 / 工作运行,如下所示,先使用nohup命令工作仍然在壳中运行,在离开壳或终端时不会被杀死。

1nohup ./hello.sh

Output Nohup command with regular commands From the output above, the output of the command has been saved to nohup.out to verify this run,

1cat nohup.out

Output Cat Nohup Out file Additionally, you can opt to redirect the output to a different file as shown

1nohup ./hello.sh > output.txt

再一次,要查看文件运行

1cat output.txt

Output Redirect Nohup Output To A text File To redirect to a file and to standard error and output use the > filename 2>&1 attribute as shown

1nohup ./hello.sh > myoutput.txt >2&1

Output Redirect to Standard Out And Standard Error

使用 Nohup 在背景中启动一个过程

要在背景中启动一个过程,请使用命令末尾的&符号. 在本示例中,我们正在 ping google.com 并将其发送到背景。

1nohup ping google.com &

Output Nohup Run Process In The Background To check the process when resuming the shell use the pgrep command as shown

1pgrep -a ping

Output Pgrep Ping Google If you want to stop or kill the running process, use the killcommand followed by the process ID as shown

1kill 2565

Output Kill PID

摘要

  1. 使用 nohup 命令运行的所有流程即使在离开壳时也会忽略 SIGHUP 信号.
  2. 使用 nohup 命令启动或执行任务后, stdin 将无法为用户使用
  3. 默认情况下,nohup.out 作为 stdout 和 stderr 的默认文件使用
Published At
Categories with 技术
Tagged with
comments powered by Disqus