JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Bash Nohup 与 & 的区别

Author:JIYIK Last Updated:2025/04/21 Views:

这篇简短的文章介绍了通过 Bash 在后台运行 Linux 进程的 nohup 命令和 & 控制运算符。 此外,我们将进一步研究 nohup 和 & 之间的主要区别。


在后台运行 Linux 进程

Linux 提供了两种在后台运行进程或命令的方法。

  1. 仅使用与号 (&) 控制运算符。
  2. 将 nohup 命令与 & 结合使用。

符号 (&) 控制运算符

我们可以使用 & 控制运算符在后台异步运行任何命令。

考虑以下命令。

sleep 10

sleep 命令添加特定时间的延迟。 当我们运行 sleep 10 命令时,它会暂停 Bash 终端 10 秒,我们不能在终端上运行任何其他命令。

现在,考虑以下命令。

sleep 10 &

上面命令显示进程id(PID),sleep 10在后台异步执行。 执行控制返回命令终端,不等待睡眠结束。

现在,我们可以在同一终端上与后台睡眠命令并发运行任何其他命令。

我们可以使用以下命令将后台进程移动到前台。

fg

nohup 命令

nohup 命令运行任何其他命令或进程。 它代表“no hang-up”,它可以防止关联的进程获得 SIGHUP 信号。

如果你想在终端关闭后仍然执行命令,你可以使用 nohup CommandName。

但是,如果我们想在后台运行一个命令,并且执行控制立即返回到终端,我们必须使用下面的命令。

nohup sleep 10 &

上面的命令在后台运行 sleep 10 命令并立即返回控制权,以便我们可以在同一终端上运行任何其他命令。

我们可以使用 pgrep 命令查看后台运行的命令,如下所示:

pgrep -a [Command]

pgrep 命令搜索命令并显示进程 ID (PID) 以及正在执行的命令详细信息。

例如,pgrep -a sleep 会将相关的后台进程显示为:

PID sleep 10

在这里,PID 表示分配给睡眠命令的进程 ID。


控制运算符 & 和 nohup 命令之间的区别

以下是使用 &nohup 在后台运行命令或进程之间的一些区别。

nohup 可以捕获挂断信号(SIGHUP),而 & 则不能。 SIGHUP 信号用于在进程启动的终端关闭时向进程发送信号。

通常,进程或命令使用 & 在后台运行,直到存在从中启动此命令或进程的 shell。 一旦 shell 终止,所有关联的命令或在后台运行的进程 & 也将终止。

当终端退出时,使用 SIGHUP (kill SIGHUP <pid>) 的挂断信号会终止该终端的所有子命令或子进程。 但是,这可以使用 nohup 来防止。

nohup 命令捕获 SIGHUP 信号,不让它到达实际命令。 因此,限制命令在 Bash 终端退出时终止。

&nohup 之间的另一个区别是关于 stdout/stderr 的重定向。

& 运算符不会自动重定向 stdout/stderr,而是将命令的输出直接显示到终端。 但是,nohup 将 stdout/stderr 重定向到位于 $HOME 的文件 nohup.out 中。

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Run a batch (.bat) file in CMD

Publish Date:2025/04/21 Views:169 Category:OPERATING SYSTEM

This article will show you how to use CMD to run a batch file.bat. There are three ways in which you can run a batch file. Let us discuss them in the following sections. Run batch (.bat) files in CMD by directly clicking on them This way yo

Running batch scripts using Task Scheduler

Publish Date:2025/04/21 Views:188 Category:OPERATING SYSTEM

This article will show you how to use Task Scheduler to run a batch file. Running batch scripts using Task Scheduler With Task Scheduler, you can automate tasks to run automatically at specific times. It only takes a few steps and you don't

Solve the error Make Command Not Found in Cygwin

Publish Date:2025/04/21 Views:75 Category:OPERATING SYSTEM

Cygwin allows Windows users to access certain Linux features and includes a large number of GNU and open source tools that are commonly found in popular Linux distributions. When using Cygwin, it is easy to encounter a command not found err

Difference between Bash Nohup and &

Publish Date:2025/04/21 Views:188 Category:OPERATING SYSTEM

This short article introduces the nohup command and the control operator to run Linux processes in the background through Bash. In addition, we will further study the key differences between nohup and . Running Linux processes in the backgr

Pull all branches in Git

Publish Date:2025/04/21 Views:98 Category:OPERATING SYSTEM

Git provides us a platform where we can maintain multiple separate development commits for a new project called branches. We can restore the latest version of a branch from a remote repository as needed or we can restore all branches at onc

Git list remote branches

Publish Date:2025/04/20 Views:55 Category:OPERATING SYSTEM

This article will show you how to list remote repositories from your local branch. A remote repository is a project hosted on a server, such as Github/Gitlab. git remote Allows us to use short names (aliases) to execute commands instead of

Update the repository remotely by setting

Publish Date:2025/04/20 Views:120 Category:OPERATING SYSTEM

In this tutorial, we will discuss how to set up the central repository as a remote for our local repository so that our branch is updated whenever the central repository changes. We should always perform this step before making edits to our

Rename Git repository

Publish Date:2025/04/20 Views:100 Category:OPERATING SYSTEM

In this article, we will discuss renaming Git repositories. We can explain this in different ways. It can rename the displayed name, the repository on GitHub, or the folder of the repository. We will discuss these and go through the steps w

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial