Linux 异步 shell 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2368137/
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
Asynchronous shell commands
提问by LorenVS
I'm trying to use a shell script to start a command. I don't care if/when/how/why it finishes. I want the process to start and run, but I want to be able to get back to my shell immediately...
我正在尝试使用 shell 脚本来启动命令。我不在乎它是否/何时/如何/为什么完成。我希望该进程启动并运行,但我希望能够立即返回到我的 shell...
采纳答案by Carl Norum
You can just run the script in the background:
您可以在后台运行脚本:
$ myscript &
Note that this is different from putting the &
inside your script, which probably won't do what you want.
请注意,这与将 放入&
脚本中不同,后者可能不会执行您想要的操作。
回答by pajton
Alternatively, after you got the program running, you can hit Ctrl-Z which stops your program and then type
或者,在程序运行后,您可以按 Ctrl-Z 停止程序,然后键入
bg
背景
which puts your last stopped program in the background. (Useful if your started something without '&' and still want it in the backgroung without restarting it)
这会将您上次停止的程序置于后台。(如果您在没有 '&' 的情况下启动了一些东西并且仍然希望它在后台而不重新启动它,则很有用)
回答by Steve B.
nohup cmd
doesn't hangup when you close the terminal. output by default goes to nohup.out
关闭终端时不会挂断。默认输出到 nohup.out
You can combine this with backgrounding,
您可以将其与背景相结合,
nohup cmd &
and get rid of the output,
并摆脱输出,
nohup cmd > /dev/null 2>&1 &
you can also disown
a command. type cmd
, Ctrl-Z
, bg
, disown
你也可以disown
一个命令。类型cmd
, Ctrl-Z
, bg
,disown
回答by Ani Menon
Everyone just forgot disown
. So here is a summary:
只是大家都忘记了disown
。所以这里是一个总结:
&
puts the job in the background.- Makes it block on attempting to read input, and
- Makes the shell not wait for its completion.
disown
removes the process from the shell's job control, but it still leaves it connected to the terminal.- One of the results is that the shell won't send it a
SIGHUP
(If the shell receives aSIGHUP
, it also sends aSIGHUP
to the process, which normally causes the process to terminate). - And obviously, it can only be applied to background jobs(because you cannot enter it when a foreground job is running).
- One of the results is that the shell won't send it a
nohup
disconnects the process from the terminal, redirects its output tonohup.out
and shields it fromSIGHUP
.- The process won't receive any sent
SIGHUP
. - Its completely independent from job control and could in principle be used also for foreground jobs(although that's not very useful).
- Usually used with
&
(as a background job).
- The process won't receive any sent
&
将工作置于后台。- 使其阻止尝试读取输入,并且
- 使外壳不等待其完成。
disown
从 shell 的作业控制中删除进程,但它仍然保持连接到终端。- 结果之一是shell不会向它发送a
SIGHUP
(如果shell收到aSIGHUP
,它也会向SIGHUP
进程发送a ,这通常会导致进程终止)。 - 显然,它只能应用于后台作业(因为在前台作业运行时您无法输入它)。
- 结果之一是shell不会向它发送a
nohup
从端子断开过程,它的输出重定向到nohup.out
和从屏蔽它SIGHUP
。- 该进程不会收到任何已发送的
SIGHUP
. - 它完全独立于作业控制,原则上也可以用于前台作业(尽管这不是很有用)。
- 通常与
&
(作为后台作业)一起使用。
- 该进程不会收到任何已发送的
回答by mluthra
screen -m -d $command$
starts the command in a detached session. You can use screen -r
to attach to the started session. It is a wonderful tool, extremely useful also for remote sessions. Read more at man screen
.
screen -m -d $command$
在分离的会话中启动命令。您可以使用screen -r
附加到已启动的会话。这是一个很棒的工具,对于远程会话也非常有用。阅读更多man screen
。