简体   繁体   English

linux 命令末尾的“&”是什么意思?

[英]What does "&" at the end of a linux command mean?

I am a system administrator and I have been asked to run a linux script to clean the system.我是一名系统管理员,我被要求运行一个 linux 脚本来清理系统。

The command is this:命令是这样的:

perl script.pl > output.log &

so this command is ending with a & sign, is there any special significance of it?所以这个命令以&结尾,有什么特殊意义吗?

I have basic knowledge of shell but I have never seen this before.我有 shell 的基本知识,但我以前从未见过这个。

The & makes the command run in the background. &使命令在后台运行。

From man bash :man bash

If a command is terminated by the control operator & , the shell executes the command in the background in a subshell.如果命令由控制运算符&终止,shell 将在子 shell 的后台执行该命令。 The shell does not wait for the command to finish, and the return status is 0. shell 不等待命令完成,返回状态为 0。

When not told otherwise commands take over the foreground.当没有被告知时,命令会接管前台。 You only have one "foreground" process running in a single shell session.您只有一个“前台”进程在单个 shell 会话中运行。 The & symbol instructs commands to run in a background process and immediately returns to the command line for additional commands. & 符号指示命令在后台进程中运行,并立即返回命令行以获取其他命令。

sh my_script.sh &

A background process will not stay alive after the shell session is closed.在 shell 会话关闭后,后台进程将不会保持活动状态。 SIGHUP terminates all running processes. SIGHUP终止所有正在运行的进程。 By default anyway.反正默认。 If your command is long-running or runs indefinitely (ie: microservice) you need to pr-pend it with nohup so it remains running after you disconnect from the session:如果您的命令长时间运行或无限期运行(即:微服务),您需要使用 nohup 对其进行 pr-pend,以便在您与会话断开连接后仍保持运行:

nohup sh my_script.sh &

EDIT: There does appear to be a gray area regarding the closing of background processes when & is used.编辑:当使用 & 时,后台进程的关闭似乎存在灰色区域。 Just be aware that the shell may close your process depending on your OS and local configurations (particularly on CENTOS/RHEL): https://serverfault.com/a/117157 .请注意,shell可能会根据您的操作系统和本地配置(特别是在 CENTOS/RHEL 上)关闭您的进程: https ://serverfault.com/a/117157。

In addition, you can use the "&" sign to run many processes through one (1) ssh connections in order to to keep minimum number of terminals.此外,您可以使用“&”符号通过一 (1) 个 ssh 连接运行多个进程,以保持最少数量的终端。 For example, I have one process that listens for messages in order to extract files, the second process listens for messages in order to upload files: Using the "&" I can run both services in one terminal, through single ssh connection to my server.例如,我有一个进程侦听消息以提取文件,第二个进程侦听消息以上传文件:使用“&”我可以在一个终端中运行这两个服务,通过单个 ssh 连接到我的服务器.


*****I just realized that these processes running through the "&" will also "stay alive" after ssh session is closed! *****我刚刚意识到,在 ssh 会话关闭后,通过“&”运行的这些进程也将“保持活动状态”! pretty neat and useful if your connection to the server is interrupted**如果您与服务器的连接中断,则非常简洁和有用**

I don't know for sure but I'm reading a book right now and what I am getting is that a program need to handle its signal ( as when I press CTRL-C ).我不确定,但我现在正在读一本书,我得到的是一个程序需要处理它的信号(就像我按下CTRL-C )。 Now a program can use SIG_IGN to ignore all signals or SIG_DFL to restore the default action.现在程序可以使用SIG_IGN忽略所有信号或使用SIG_DFL恢复默认操作。

Now if you do $ command & then this process running as background process simply ignores all signals that will occur.现在,如果你执行$ command &那么这个作为后台进程运行的进程会忽略所有将发生的信号。 For foreground processes these signals are not ignored.对于前台进程,这些信号不会被忽略。

If you have a command which executes and doesn't return status 0(control of prompt) quickly.如果您有一个命令执行并且不会快速返回状态 0(提示控制)。

For example:例如:

  1. command gedit launches the default editor gedit UI.命令gedit启动默认编辑器gedit UI。

  2. command eclipse launches eclipse IDE.命令eclipse启动 eclipse IDE。

Such commands keep throwing the logs of activities in the terminal and don't return the command prompt.此类命令会不断在终端中抛出活动日志,并且不会返回命令提示符。

Question is, how to run such commands in background so that, we will get back command terminal and we can use terminal for other tasks.问题是,如何在后台运行这些命令,以便我们返回命令终端,我们可以使用终端执行其他任务。

Answer is: by appending & after such command.答案是:通过在这样的命令之后附加&

 user@mymachine:~$ <command> &

Examples:例子:

user@mymachine:~$ <command> gedit &
user@mymachine:~$ <command> eclipse &

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM