Linux 如何在后台运行 shell 脚本而没有输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9190151/
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
How to run a shell script in the background and get no output
提问by Behnam Safari
I wrote two shell scripts a.sh
and b.sh
. In a.sh
and b.sh
I have a infinite for loop and they print some output to the terminal. I want to write another script which calls both a.sh
and b.sh
but I want the user to regain control of the terminal immediately, instead of having the script run infinitely and I want to hide the output in terminal.
我写了两个 shell 脚本a.sh
和b.sh
. 在a.sh
,b.sh
我有一个无限循环,他们将一些输出打印到终端。我想编写另一个脚本来调用两者a.sh
,b.sh
但我希望用户立即重新获得对终端的控制权,而不是让脚本无限运行并且我想在终端中隐藏输出。
采纳答案by George
Use nohup
if your background job takes a long time to finish or you just use SecureCRT or something like it login the server.
使用nohup
,如果你的后台作业需要较长时间才能完成,或者你只是使用SecureCRT的或类似的东西登录服务器。
Redirect the stdoutand stderrto /dev/null
to ignore the output.
将stdout和stderr重定向到/dev/null
以忽略输出。
nohup /path/to/your/script.sh > /dev/null 2>&1 &
回答by Michael Chinen
If they are in the same directory as your script that contains:
如果它们与包含以下内容的脚本位于同一目录中:
./a.sh > /dev/null 2>&1 &
./b.sh > /dev/null 2>&1 &
The &
at the end is what makes your script run in the background.
将&
在到底是什么让你的脚本在后台运行。
The > /dev/null 2>&1
part is not necessary - it redirects the stdout and stderr streams so you don't have to see them on the terminal, which you may want to do for noisy scripts with lots of output.
该> /dev/null 2>&1
部分不是必需的 - 它重定向 stdout 和 stderr 流,因此您不必在终端上看到它们,对于具有大量输出的嘈杂脚本,您可能想要这样做。
回答by Adam Zalcman
Redirect the output to a file like this:
将输出重定向到这样的文件:
./a.sh > somefile 2>&1 &
This will redirect both stdout and stderr to the same file. If you want to redirect stdout and stderr to two different files use this:
这会将 stdout 和 stderr 重定向到同一个文件。如果要将 stdout 和 stderr 重定向到两个不同的文件,请使用以下命令:
./a.sh > stdoutfile 2> stderrfile &
You can use /dev/null
as one or both of the files if you don't care about the stdout and/or stderr.
/dev/null
如果您不关心标准输出和/或标准错误,您可以将其用作其中一个或两个文件。
See bash manpagefor details about redirections.
有关重定向的详细信息,请参阅bash 联机帮助页。
回答by DanteAlighieri
Sorry this is a bit late but found the ideal solution for somple commands where you don't want any standard or error output (credit where it's due: http://felixmilea.com/2014/12/running-bash-commands-background-properly/)
抱歉,这有点晚了,但找到了不想要任何标准或错误输出的简单命令的理想解决方案(信用到期:http: //felixmilea.com/2014/12/running-bash-commands-background -正确/)
This redirects output to null and keeps screen clear:
这会将输出重定向到 null 并保持屏幕清晰:
command &>/dev/null &
回答by vaquar khan
nohup sh -x runShellScripts.sh &
回答by Tom Hale
Run in a subshell to remove notifications and close STDOUT and STDERR:
在子 shell 中运行以删除通知并关闭 STDOUT 和 STDERR:
(&>/dev/null script.sh &)
回答by Ramin Ismayilli
These examples work fine:
这些示例工作正常:
nohup sh prog.sh proglog.log 2>&1 &
nohup sh prog.sh proglog.log 2>&1 &
For loop you can use like this :
For 循环,你可以这样使用:
for i in {1..10}; do sh prog.sh; sleep 1; done prog.log 2>&1 &
对于 {1..10} 中的 i;做 sh prog.sh; 睡眠 1; 完成 prog.log 2>&1 &
It works in background and does not show any output.
它在后台工作,不显示任何输出。
回答by SARATH CHANDRAN
If you want to run the script in a linux kickstart you have to run as below .
如果您想在 linux kickstart 中运行脚本,您必须按如下方式运行。
sh /tmp/script.sh > /dev/null 2>&1 < /dev/null &