当你离开Linux系统的壳时,所有运行过程通常会被终止或关闭,所以你要做什么如果你仍然想要保持流程的运行,即使离开壳/终端?这就是nohup
命令出现的地方。
Nohup指挥
Nohup是Linux系统中的一个命令,即使在离开壳或终端后也保持流程运行。Nohup阻止流程或工作接收SIGHUP(信号挂起)信号。这是在关闭或离开终端时发送给进程的信号。
Nohup 命令语法
Nohup命令语法如下:
1nohup command arguments
或
1nohup options
让我们看看命令如何进入游戏
查看 Nohup 的版本
您可以通过使用下面的语法检查Nohup的版本开始。
1nohup --version
Output
使用 Nohup 启动一个过程
如果你想保持你的流程 / 工作运行,如下所示,先使用nohup
命令工作仍然在壳中运行,在离开壳或终端时不会被杀死。
1nohup ./hello.sh
Output From the output above, the output of the command has been saved to
nohup.out
to verify this run,
1cat nohup.out
Output Additionally, you can opt to redirect the output to a different file as shown
1nohup ./hello.sh > output.txt
再一次,要查看文件运行
1cat output.txt
Output 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
使用 Nohup 在背景中启动一个过程
要在背景中启动一个过程,请使用命令末尾的&
符号. 在本示例中,我们正在 ping google.com 并将其发送到背景。
1nohup ping google.com &
Output To check the process when resuming the shell use the
pgrep
command as shown
1pgrep -a ping
Output If you want to stop or kill the running process, use the
kill
command followed by the process ID as shown
1kill 2565
Output
摘要
- 使用 nohup 命令运行的所有流程即使在离开壳时也会忽略 SIGHUP 信号.
- 使用 nohup 命令启动或执行任务后, stdin 将无法为用户使用
- 默认情况下,nohup.out 作为 stdout 和 stderr 的默认文件使用