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

如果你是一个忙碌的人,那么你最不需要的就是被大量“无用”的通知所困扰,那么你如何让事情平静下来呢?今天的超级用户问答有一些很好的答案,可以帮助读者降低输出量。...

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

如果你是一个忙碌的人,那么你最不需要的就是被大量“无用”的通知所困扰,那么你如何让事情平静下来呢?今天的超级用户问答有一些很好的答案,可以帮助读者降低输出量。

今天的问答环节是由SuperUser提供的,SuperUser是Stack Exchange的一个分支,是一个由社区驱动的问答网站分组。

问题

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

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

如何让命令在后台运行而不输出,除非有错误?

答案

超级用户贡献者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.

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.

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.

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):

007Ys3FFgy1gpecq2ldyfj30hd02rmx3

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

007Ys3FFgy1gpecq35l17j30hd0260sn

Or:

007Ys3FFgy1gpecq43z79j30hd00wt8l

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

(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.)

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:

007Ys3FFgy1gpecq4ocbij30hd03yq2v

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

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!

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

接着是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 c***ole.

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 c***ole.

So for instance:

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

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


有什么要补充的解释吗?在评论中发出声音。想从其他精通技术的Stack Exchange用户那里了解更多答案吗?在这里查看完整的讨论主题。

  • 发表于 2021-04-10 08:39
  • 阅读 ( 215 )
  • 分类:互联网

你可能感兴趣的文章

如何在带屏幕的linux终端上进行多任务处理

... 如何安装屏幕 ...

  • 发布于 2021-03-21 22:09
  • 阅读 ( 193 )

如何检查windows 10 pc的运行状况(并保持其状态)

... 下面是如何使用这些报告来了解硬件的运行情况,以及可能需要修复的特定问题的一些提示和解决方法。你还有什么建议吗?在评论中与我们分享! ...

  • 发布于 2021-03-23 14:44
  • 阅读 ( 377 )

linux中的进程是什么?

... 流程如何开始 ...

  • 发布于 2021-03-27 02:21
  • 阅读 ( 235 )

windows 10更新后速度慢吗?下面是你可以修复它的方法

...误的更新 损坏的系统文件 后台应用程序 ...

  • 发布于 2021-03-27 04:04
  • 阅读 ( 454 )

如何使用crontab自动化linux中的重复任务

... 在这里,您将了解什么是crontab,如何添加自动化作业,以及一些实际示例。 ...

  • 发布于 2021-03-28 04:45
  • 阅读 ( 207 )

如何修复windows中dpc看门狗冲突停止码错误

...疑是其中之一。如果您遇到了DPC看门狗违规错误,下面是如何修复它的。 ...

  • 发布于 2021-03-29 21:00
  • 阅读 ( 342 )

如何在linux上杀死僵尸进程

...好的程序会让僵尸进程潜伏在Linux计算机中。了解僵尸是如何产生的,以及如何让它们最终安息。 进程状态如何在linux上工作 当然,Linux必须跟踪计算机上运行的所有应用程序和守护进程。其中一种方法是维护进程表。这是内...

  • 发布于 2021-04-01 07:47
  • 阅读 ( 234 )

如何使用linux的screen命令

...窗口推到后台(称为“分离”)。当你想看到你的进程是如何进行的,你可以把窗口拉到前台(“重新连接”)并再次使用它。这对于您不希望通过关闭终端窗口意外终止的长进程来说非常有用。 一旦运行了屏幕会话,就可以...

  • 发布于 2021-04-02 11:06
  • 阅读 ( 174 )

如何在linux上使用at和batch来调度命令

...只想在系统有空闲资源时运行进程,可以使用批处理。 如何安排linux作业 cron守护进程维护它在特定时间运行的作业列表。这些任务和程序按预定时间在后台运行。这为您安排需要重复的任务提供了极大的灵活性。无论您需要...

  • 发布于 2021-04-02 18:33
  • 阅读 ( 185 )

如何在linux上使用dmesg命令

...和术语。按less中的正斜杠“/”键启动搜索功能。 相关:如何在Linux上使用less命令 不再需要sudo 如果希望避免每次使用dmesg时都必须使用sudo,可以使用以下命令。但是,请注意:它允许任何拥有您的计算机用户帐户的人使用dmesg...

  • 发布于 2021-04-02 21:09
  • 阅读 ( 219 )
May有你
May有你

0 篇文章

相关推荐