如何在後臺執行沒有輸出的命令,除非出現錯誤?

如果你是一個忙碌的人,那麼你最不需要的就是被大量“無用”的通知所困擾,那麼你如何讓事情平靜下來呢?今天的超級使用者問答有一些很好的答案,可以幫助讀者降低輸出量。...

如何在後臺執行沒有輸出的命令,除非出現錯誤?

如果你是一個忙碌的人,那麼你最不需要的就是被大量“無用”的通知所困擾,那麼你如何讓事情平靜下來呢?今天的超級使用者問答有一些很好的答案,可以幫助讀者降低輸出量。

今天的問答環節是由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
  • 閱讀 ( 42 )
  • 分類:網際網路

你可能感興趣的文章

如何防止應用程式在Windows10中後臺執行

...dows10的現代應用程式可以在你電腦的後臺執行。下面介紹如何防止它們這樣做。 ...

  • 發佈於 2021-03-11 23:03
  • 閲讀 ( 36 )

如何隱藏android oreo的“後臺執行”通知

androidoreo現在可用於部分裝置,並帶來了一些增量但明顯的變化。其中之一是一個新的永續性通知,它可以讓你知道應用程式何時在後臺執行。 ...

  • 發佈於 2021-03-13 02:52
  • 閲讀 ( 31 )

Windows10中使用的7個漂亮的chkdsk功能

... 我們已經寫了關於如何使用CHKDSK的文章,所以如果你想開始的話就跳過去吧。這篇文章是關於Windows8和Windows10中的CHKDSK工具如何比Windows7中的CHKDSK工具有了巨大的改進。 ...

  • 發佈於 2021-03-16 17:54
  • 閲讀 ( 42 )

聽上去你的mac不工作?輕鬆解決音訊問題

...jave是最後一個支援32位應用程式的版本。我們已經介紹瞭如何在升級之前檢查Mac上的32位應用程式。 ...

  • 發佈於 2021-03-18 16:27
  • 閲讀 ( 52 )

如何在帶螢幕的linux終端上進行多工處理

... 如何安裝螢幕 ...

  • 發佈於 2021-03-21 22:09
  • 閲讀 ( 38 )

8個簡單的補丁,當你的手機應用程式在Windows10中不工作

... 標題1:你的**壞了?下面介紹如何在windows10和Android上修復它 ...

  • 發佈於 2021-03-26 20:41
  • 閲讀 ( 63 )

linux中的程序是什麼?

... 流程如何開始 ...

  • 發佈於 2021-03-27 02:21
  • 閲讀 ( 59 )

如何使用crontab自動化linux中的重複任務

... 在這裡,您將瞭解什麼是crontab,如何新增自動化作業,以及一些實際示例。 ...

  • 發佈於 2021-03-28 04:45
  • 閲讀 ( 53 )

如何在關閉microsoft edge時阻止後臺應用程式執行

...執行。這意味著Edge將繼續使用您計算機的資源。下面是如何停止後臺程序。 什麼是後臺應用程式(background apps)? 預設情況下,Microsoft Edge允許後臺應用程式和擴充套件在計算機的後臺執行。這使得需要全天候執行的擴充套件和...

  • 發佈於 2021-04-02 14:51
  • 閲讀 ( 123 )

如何在linux上使用at和batch來排程命令

...只想在系統有空閒資源時執行程序,可以使用批處理。 如何安排linux作業 cron守護程序維護它在特定時間執行的作業列表。這些任務和程式按預定時間在後臺執行。這為您安排需要重複的任務提供了極大的靈活性。無論您需要...

  • 發佈於 2021-04-02 18:33
  • 閲讀 ( 68 )
May有你
May有你

0 篇文章

作家榜

  1. admin 0 文章
  2. 孫小欽 0 文章
  3. JVhby0 0 文章
  4. fvpvzrr 0 文章
  5. 0sus8kksc 0 文章
  6. zsfn1903 0 文章
  7. w91395898 0 文章
  8. SuperQueen123 0 文章

相關推薦