scp命令输出日志后台执行_除非出现错误,如何在没有输出的情况下在后台运行命令?...

scp命令输出日志后台执行

scp命令输出日志后台执行

run-a-command-in-the-background-with-no-output-unless-there-is-an-error-00

If you are a busy person, then the last thing you need is to be bothered with a huge amount of ‘useless’ notifications, so how do you quieten things down? Today’s SuperUser Q&A post has some great answers to help a reader quieten down the amount of output.

如果您是一个忙碌的人,那么您需要做的最后一件事就是打扰大量的“无用”通知,那么您如何使事情安静下来? 今天的“超级用户”问答页面提供了一些不错的答案,可以帮助读者降低输出量。

Today’s Question & Answer session comes to us courtesy of SuperUser—a subdivision of Stack Exchange, a community-driven grouping of Q&A web sites.

今天的“问答”环节由SuperUser提供,它是Stack Exchange的一个分支,该社区是由社区驱动的Q&A网站分组。

问题 (The Question)

SuperUser reader Xster wants to know how to run a command in the background with no output unless there is an error:

超级用户阅读器Xster想要知道如何在没有输出的情况下在后台运行命令,除非出现错误:

How do you suppress a command’s output, but show it if the command’s exit codes an error?

您如何抑制命令的输出,但是如果命令的退出编码为错误,则如何显示呢?

How do you get a command to run in the background with no output unless there is an error?

除非出现错误,如何获得没有输出的后台运行命令?

答案 (The Answer)

SuperUser contributors Bob and Maximillian Laumeister have the answer for us. First up, Bob:

超级用户贡献者Bob和Maximillian Laumeister为我们提供了答案。 首先,鲍勃:

Unfortunately, the assumption that stderr is only used for error output is not always correct. Rather, stderr is often used for any and all interactive output and diagnostics (i.e. output intended for the user to read in an interactive prompt).(1) wget and dd are well-known examples.

不幸的是, stderr仅用于错误输出的假设并不总是正确的。 相反, stderr通常用于所有交互式输出和诊断(即供用户在交互式提示中阅读的输出)。 (1) wgetdd是众所周知的示例。

Some commands will provide a flag (i.e. -quiet or -silent) to suppress non-error output. Read their man pages to see if one exists.

一些命令将提供一个标志(即-quiet-silent )来禁止非错误输出。 阅读他们的手册页,看看是否存在。

Another convention that holds more often is the exit code, a program returns an exit code when it exits. Typically(2), an exit code of 0 indicates success, and any other exit code indicates an error.

另一个更常用的约定是退出代码,程序退出时会返回退出代码。 通常, (2)的退出代码为0表示成功,而其他任何退出代码则表示错误。

With bash, you can get the exit code of the last command from the $? variable. In fish, use the $status variable. You can pipe stderr to a temporary file and only print it if an error occurs. For example (fish):

使用bash ,您可以从$?获得最后一个命令的退出代码 变量。 在fish中,使用$ status变量。 您可以将stderr用管道传输到临时文件,并且仅在发生错误时才打印。 例如():

run-a-command-in-the-background-with-no-output-unless-there-is-an-error-01

You can also use some shortcuts if you are not chaining commands:

如果不链接命令,也可以使用一些快捷方式:

run-a-command-in-the-background-with-no-output-unless-there-is-an-error-02

Or:

要么:

run-a-command-in-the-background-with-no-output-unless-there-is-an-error-03

You can also pipe stdout to the same buffer by using 2>&1 >/tmp/outputbuffer.

您还可以使用2>&1> / tmp / outputbuffer将stdout传递到同一缓冲区。

(Note: I do not actually know fish, so I am adapting the concept to what I can find in its documentation. The syntax might be slightly wrong. Also, you can use mktemp to generate a unique temporary file. Run it and record the file name in a variable.)

(注意:我实际上并不了解fish ,所以我正在对该概念进行调整以使其可以在其文档中找到。语法可能有些错误。此外,您可以使用mktemp生成唯一的临时文件。运行它并记录下变量中的文件名。)

If you need to run the whole thing in the background of a shell that you are also using interactively at the same time, then you are better off writing a script to handle the output-hiding and running that script in the background with the standard techniques (fish). Heck, you can put something like the following function in ~/.config/fish/config.fish:

如果您需要同时在交互使用的shell后台运行整个程序,那么最好编写一个脚本来处理输出隐藏,并使用标准技术在后台运行该脚本()。 哎呀,您可以在〜/ .config / fish / config.fish中添加类似以下功能的内容

run-a-command-in-the-background-with-no-output-unless-there-is-an-error-04

Call with run-silent somecommand & (where the trailing & causes it to run in the background)

使用run-silent somecommand调用& (尾随使其在后台运行)

Note that this will swallow the original exit code, and will dump both stdout and stderr in the event of a failure. You can customise it as necessary.

请注意,这将吞噬原始的退出代码,并在发生故障时转储标准输出标准错误。 您可以根据需要自定义它。

(1) There is no guarantee that error output will not appear on stdout, some programs will dump all output there!

(1)无法保证错误输出不会出现在stdout上,某些程序会将所有输出转储到那里!

(2) Unfortunately, this is still not always the case. The exit code is completely controlled by the program and some will indicate some success conditions with non-zero exits. Again, check the manual.

(2)不幸的是,情况并非总是如此。 退出代码完全由程序控制,有些将指示非零退出的成功条件。 再次,检查手册。

Followed by the answer from Maximillian Laumeister:

接下来是马克西米利安·劳梅斯特(Maximillian Laumeister)的回答:

Unix utilities send general messages to stdout, and error messages to stderr, so if we only want to see error messages, then it will be sufficient to suppress stdout so that only stderr gets output to the console.

Unix实用程序将常规消息发送到stdout ,将错误消息发送到stderr ,因此,如果我们只想查看错误消息,那么抑制stdout就足够了,这样只有stderr才能输出到控制台。

The way to do this (in both bash and fish) is to append >/dev/null to the command. This pipes stdout into nothingness, but stderr (with your error messages) still comes through to the console.

(在bashfish中)执行此操作的方法是将> / dev / null附加到命令。 这会将stdout传递到虚无状态,但是stderr (带有您的错误消息)仍然可以到达控制台。

So for instance:

因此,例如:

The command echo 1 >/dev/null prints nothing, because the normal stdout output is suppressed, and nothing was written to stderr.

命令echo 1> / dev / null不输出任何内容,因为正常的stdout输出被抑制,并且没有任何内容写入stderr

The command man doesnotexist >/dev/null prints an error message, because man writes its error message to stderr.

man mannotenotexist> / dev / null命令将显示错误消息,因为man将其错误消息写入stderr



Have something to add to the explanation? Sound off in the comments. Want to read more answers from other tech-savvy Stack Exchange users? Check out the full discussion thread here.

有什么补充说明吗? 在评论中听起来不错。 是否想从其他精通Stack Exchange的用户那里获得更多答案? 在此处查看完整的讨论线程

翻译自: https://www.howtogeek.com/223850/how-do-you-run-a-command-in-the-background-with-no-output-unless-there-is-an-error/

scp命令输出日志后台执行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值