linux中nohup命令
When exiting the shell of a Linux System, all running processes are usually terminated or hang up. So what do you do If you still want to keep the processes running even exiting the shell/terminal? This is where the nohup command comes in.
退出Linux系统的外壳时,所有正在运行的进程通常都会终止或挂断。 那么,如果您仍要保持进程运行(甚至退出shell /终端),该怎么办? 这是nohup命令出现的地方。
Nohup命令 (Nohup Command)
Nohup, short for no hang up is a command in Linux systems that keep processes running even after exiting the shell or terminal.
Nohup(不挂机的简称)是Linux系统中的一个命令,即使退出shell或终端后,该命令也可使进程保持运行状态。
Nohup prevents the processes or jobs from receiving the SIGHUP (Signal Hang UP) signal. This is a signal that is sent to a process upon closing or exiting the terminal. In this guide, we take a look at the nohup command and demonstrate how it can be used.
Nohup阻止进程或作业接收SIGHUP(信号挂起)信号。 这是在关闭或退出终端时发送到进程的信号。 在本指南中,我们看一下nohup命令并演示如何使用它。
Nohup命令语法 (Nohup Command Syntax)
Nohup command syntax is as follows;
Nohup命令的语法如下:
nohup command arguments
OR
要么
nohup options
Let’s see how the command comes into play
让我们看看命令如何发挥作用
检查Nohup的版本 (Checking the version of Nohup)
You can begin by checking the version of Nohup using the syntax below
您可以使用以下语法从检查Nohup的版本开始
nohup --version
Output
输出量
使用Nohup启动进程 (Starting a process using Nohup)
If you want to keep your processes/jobs running, precede the command with nohup
as shown below. The jobs will still continue running in the shell and will not get killed upon exiting the shell or terminal.
如果要保持进程/作业运行,请在命令之前添加nohup
,如下所示。 这些作业仍将继续在Shell中运行,并且在退出Shell或终端时不会被杀死。
nohup ./hello.sh
Output
输出量
From the output above, the output of the command has been saved to nohup.out
to verify this run,
在上面的输出中,命令的输出已保存到nohup.out
以验证此运行,
cat nohup.out
Output
输出量
Additionally, you can opt to redirect the output to a different file as shown
此外,您可以选择将输出重定向到另一个文件,如图所示
nohup ./hello.sh > output.txt
Once again, to view the file run
再次,查看文件运行
cat output.txt
Output
输出量
To redirect to a file and to standard error and output use the > filename 2>&1
attribute as shown
要将文件重定向到标准错误并输出,请使用> filename 2>&1
属性,如下所示
nohup ./hello.sh > myoutput.txt >2&1
Output
输出量
使用Nohup在后台启动进程 (Starting a process in the background using Nohup)
To start a process in the background use the &
symbol at the end of the command. In this example, we are pinging google.com and sending it to the background.
要在后台启动进程,请在命令末尾使用&
符号。 在此示例中,我们要ping google.com并将其发送到后台。
nohup ping google.com &
Output
输出量
To check the process when resuming the shell use the pgrep
command as shown
要在恢复外壳程序时检查进程,请使用pgrep
命令,如下所示
pgrep -a ping
Output
输出量
If you want to stop or kill the running process, use the kill
command followed by the process ID as shown
如果要停止或终止正在运行的进程,请使用kill
命令,后跟进程ID,如下所示
kill 2565
Output
输出量
摘要 (Summary)
- All processes that are run using the nohup command will ignore the SIGHUP signal even upon exiting the shell. 使用nohup命令运行的所有进程,即使退出shell也会忽略SIGHUP信号。
- Once a job is started or executed using the nohup command, stdin will not be available to the user. 使用nohup命令启动或执行作业后,用户将无法使用stdin。
- By default, the
nohup.out
is used as the default file for stdout and stderr. 默认情况下,nohup.out
用作stdout和stderr的默认文件。
翻译自: https://www.journaldev.com/27875/nohup-command-in-linux
linux中nohup命令