2016-02-28 99 views
0

&附加到命令的结尾会在后台启动它。例如:安静地在后台启动进程

$ wget google.com & 
[1] 7072 

但是,这会打印一个作业号和PID。是否有可能防止这些?

注:我仍然想保留wget的输出,这只是我想摆脱的[1] 7072

+0

** $> wget input&> NIL:**将输出发送到bin(删除)。 –

+0

@ArifBurhan这将创建一个名为'NIL'的文件,不会将输出压缩到标准错误,而且该命令不会生成作业编号/ PID消息,因此这不起作用。 –

+0

他的意思是'wget google.com&>/dev/null' –

回答

1

有一个选项到set内置,set -b,控制该线的输出,但选择限制为“立即”(设置时间)和“等待下一个提示”(当未设置)。即时打印的

例如,当选项设置:

$ set -b 
$ sleep 1 & 
[1] 9696 
$ [1]+ Done     sleep 1 

和平常的行为,等待下一个提示:

$ set +b 
$ sleep 1 & 
[1] 840 
$ # Press enter here 
[1]+ Done     sleep 1 

所以就我所看到的,这些都可以”不被压制。可喜的是,虽然,作业控制消息不显示在非交互的shell:

$ cat sleeptest 
#!/bin/bash 
sleep 1 & 
$ ./sleeptest 
$ 

所以,如果你在后台在子shell启动命令,不会有任何消息。要做到这一点在交互式会话,你可以在这样的子shell(感谢David C. Rankin)运行命令:

$ (sleep 1 &) 
$ 

,这也导致在没有作业控制提示。

+0

嗯。非交互式shell可能工作... –

-1

Advanced Bash-Scripting Guide

抑制stdout

cat $filename >/dev/null 
# Contents of the file will not list to stdout. 

抑制stderr(来自Example 16-3)。

rm $badname 2>/dev/null 
#   So error messages [stderr] deep-sixed. 
Suppressing output from both stdout and stderr. 
cat $filename 2>/dev/null >/dev/null 
#1 If "$filename" does not exist, there will be no error message   output. 
# If "$filename" does exist, the contents of the file will not list to stdout. 
# Therefore, no output at all will result from the above line of code. 
# 
# This can be useful in situations where the return code from a command 
#+ needs to be tested, but no output is desired. 
# 
# cat $filename &>/dev/null 
#  also works, as Baris Cicek points out. 
+0

我仍然希望stdout/stderr正常;这只是我不想要的工作号码和PID。 –