在后台安静地启动进程
Start process in background quietly
在命令末尾附加 &
会在后台启动它。例如:
$ wget google.com &
[1] 7072
但是,这会打印作业编号和 PID。有可能避免这些吗?
注意:我仍然想保留 wget
的输出 - 它只是我想摆脱的 [1] 7072
。
来自Advanced Bash-Scripting Guide:
Suppressing stdout
.
cat $filename >/dev/null
# Contents of the file will not list to stdout.
Suppressing stderr
(from 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.
内置 set
的选项 set -b
控制该行的输出,但选择仅限于 "immediately"(设置时)和 "wait for next prompt"(未设置时)。
设置选项后立即打印的示例:
$ 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 & )
$
这也会导致没有作业控制提示。
在命令末尾附加 &
会在后台启动它。例如:
$ wget google.com &
[1] 7072
但是,这会打印作业编号和 PID。有可能避免这些吗?
注意:我仍然想保留 wget
的输出 - 它只是我想摆脱的 [1] 7072
。
来自Advanced Bash-Scripting Guide:
Suppressing
stdout
.cat $filename >/dev/null # Contents of the file will not list to stdout.
Suppressing
stderr
(from 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.
内置 set
的选项 set -b
控制该行的输出,但选择仅限于 "immediately"(设置时)和 "wait for next prompt"(未设置时)。
设置选项后立即打印的示例:
$ 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 & )
$
这也会导致没有作业控制提示。