
在 Linux 上,當從終端機運行圖形程式(例如 Sublime Text)時,程式運作良好,但在控制台上記錄的訊息並不完全是失敗,也不是有用的日誌訊息,只是“內容”,例如:
(sublime:15269): Gtk-WARNING **: Unable to locate theme engine in module_path: "oxygen-gtk",
(sublime:15269): GLib-CRITICAL **: Source ID 958 was not found when attempting to remove it
....在背景執行程式並繼續使用終端時會出現問題。
刪除此內容的最佳做法是什麼?
我想用一個包裝二進制啟動但抑制所有輸出()的腳本替換二進位文件> /dev/null 2>&1
,但是沒有更乾淨的方法嗎?
答案1
在 bash 中,您可以>/dev/null 2>&1
使用 來完成&>/dev/null
。
你可以這樣做:
#Run a command in the background, ignoring its STDOUT and STDERR
silence() { local cmd="$1"; shift; "$cmd" "$@" &>/dev/null & }
#The same, but stop caring about it too (no job control, no SIGHUP when the parent terminal closes)
abandon() { silence "$@"; disown; }
然後你會這樣做:
silence sublime_text
或者
abandon sublime_text
取決於您是否仍然想輕鬆地從終端控制 Sublime Text。
($@ 的魔力是處理較複雜的參數)