linux命令末尾的“&”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13338870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does "&" at the end of a linux command mean?
提问by Dude
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 的基本知识,但我以前从未见过这个。
采纳答案by mgarciaisaia
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. The shell does not wait for the command to finish, and the return status is 0.
如果命令由控制运算符&终止,shell 将在子 shell 的后台执行该命令。shell 不等待命令完成,返回状态为 0。
回答by Zack S
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. 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.
*****I just realized that these processes running through the "&" will also "stay alive" after ssh session is closed! pretty neat and useful if your connection to the server is interrupted**
此外,您可以使用“&”符号通过一 (1) 个 ssh 连接运行多个进程,以保持最少数量的终端。例如,我有一个进程侦听消息以提取文件,第二个进程侦听消息以上传文件:使用“&”我可以在一个终端中运行这两个服务,通过单个 ssh 连接到我的服务器.
*****我刚刚意识到,在 ssh 会话关闭后,通过“&”运行的这些进程也将“保持活动状态”!如果您与服务器的连接中断,则非常简洁和有用**
回答by Timothy Jannace
When not told otherwise commands take over the foreground. You only have one "foreground" process running in a single shell session. The & symbol instructs commands to run in a background process and immediately returns to the command line for additional commands.
当没有被告知时,命令会接管前台。您只有一个“前台”进程在单个 shell 会话中运行。& 符号指示命令在后台进程中运行,并立即返回命令行以获取其他命令。
sh my_script.sh &
A background process will notstay alive after the shell session is closed. SIGHUPterminates all running processes. 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:
在 shell 会话关闭后,后台进程将不会保持活动状态。SIGHUP终止所有正在运行的进程。反正默认。如果您的命令长时间运行或无限期运行(即:微服务),您需要使用 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 mayclose 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 。
回答by Kaustubh Kislay
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
). Now a program can use SIG_IGN
to ignore all signals or SIG_DFL
to restore the default action.
我不确定,但我现在正在读一本书,我得到的是一个程序需要处理它的信号(当我按下时CTRL-C
)。现在程序可以SIG_IGN
用来忽略所有信号或SIG_DFL
恢复默认操作。
Now if you do $ command &
then this process running as background process simply ignores all signals that will occur. For foreground processes these signals are not ignored.
现在,如果你这样做,$ command &
那么这个作为后台进程运行的进程会忽略所有将发生的信号。对于前台进程,这些信号不会被忽略。