簡體   English   中英

如何在程序輸出流管道到另一個程序之前延遲或緩沖程序輸出流幾秒鍾?

[英]How do i delay or buffer the stream of a programs output for a few seconds before it pipes to another program?

我想運行一個程序,幾秒鍾后會在stderr中顯示生成的url。 我想把這個網址傳遞給我的瀏覽器。 我還想保持終端的輸出不變,所以我使用了tee命令。

我已經通過將輸出轉換為文件解決了所有解析和管道路徑。 但仍需要弄清楚如何將其鏈接到程序本身。

michael@DESKTOP-OI3AOU6:~$ ./anaconda3/bin/jupyter lab ~ 2> 1.txt

michael@DESKTOP-OI3AOU6:~$ cat 1.txt
[I 12:02:11.619 NotebookApp] JupyterLab extension loaded from /home/michael/anaconda3/lib/python3.7/site-packages/jupyterlab
[I 12:02:11.620 NotebookApp] JupyterLab application directory is /home/michael/anaconda3/share/jupyter/lab
[I 12:02:11.622 NotebookApp] Serving notebooks from local directory: /home/michael/anaconda3/bin
[I 12:02:11.622 NotebookApp] The Jupyter Notebook is running at:
[I 12:02:11.622 NotebookApp] http://localhost:8888/?token=e48288141f435ebe3008ba9209d2c6d4f456a664bf6aed34
[I 12:02:11.622 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 12:02:11.631 NotebookApp]

    To access the notebook, open this file in a browser:
        file:///home/michael/.local/share/jupyter/runtime/nbserver-2069-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=e48288141f435ebe3008ba9209d2c6d4f456a664bf6aed34

然后我可以通過我的鏈管道它:

michael@DESKTOP-OI3AOU6:~$ cat 1.txt > >(grep ^[[:blank:]].*http.* | tr -d " \t\n\r")
michael@DESKTOP-OI3AOU6:~$ http://localhost:8888/?token=e48288141f435ebe3008ba9209d2c6d4f456a664bf6aed34

並且通過自定義配置文件設置管理網址到我的瀏覽器非常有用:

cat 1.txt > >(grep ^[[:blank:]].*http.* | tr -d " \\t\\n\\r" | xargs firefox.exe -P jupyterlab 2> /dev/null)

把它放在一起我得到了我想要的瀏覽器啟動行為和錯誤日志顯示:

michael@DESKTOP-OI3AOU6:~$ cat 1.txt > >(tee >(grep ^[[:blank:]].*http.* | tr -d " \\t\\n\\r"| xargs firefox.exe -P jupyterlab 2>/dev/null))

michael@DESKTOP-OI3AOU6:~$ [I 12:02:11.619 NotebookApp] JupyterLab extension loaded from /home/michael/anaconda3/lib/python3.7/site-packages/jupyterlab
[I 12:02:11.620 NotebookApp] JupyterLab application directory is /home/michael/anaconda3/share/jupyter/lab
[I 12:02:11.622 NotebookApp] Serving notebooks from local directory: /home/michael/anaconda3/bin
[I 12:02:11.622 NotebookApp] The Jupyter Notebook is running at:
[I 12:02:11.622 NotebookApp] http://localhost:8888/?token=e48288141f435ebe3008ba9209d2c6d4f456a664bf6aed34
[I 12:02:11.622 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 12:02:11.631 NotebookApp]

    To access the notebook, open this file in a browser:
        file:///home/michael/.local/share/jupyter/runtime/nbserver-2069-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=e48288141f435ebe3008ba9209d2c6d4f456a664bf6aed34

當我將程序附加到管道時會出現問題。 初始化服務器並輸出輸出需要幾秒鍾。 tr命令最終只是將一個空字符串向前推送到瀏覽器,然后由grep接收到正確的行。

michael@DESKTOP-OI3AOU6:~$ ./anaconda3/bin/jupyter lab ~ 2> >(tee >(gre p ^[[:blank:]].*http.* | tr -d " \\t\\n\\r"| xargs firefox.exe -P jupyterlab 2>/dev/null))

它適用於grep,但是url只會在加載后幾秒鍾后顯示:

michael@DESKTOP-OI3AOU6:~$ ./anaconda3/bin/jupyter lab ~ 2> >(grep ^[[:blank:]].*http.*)
        http://localhost:8888/?token=6988d45b9baa2e9f07c5a91a9a91457d6119e9884bdbcb10

我沒有出現任何東西。

michael@DESKTOP-OI3AOU6:~$ ./anaconda3/bin/jupyter lab ~ 2> >(grep ^[[:blank:]].*http.* | tr -d " \\t\\n\\r")

如何讓命令(grep)等待幾秒鍾才能將它發送到流鏈(tr)中的下一個命令?

我能夠通過簡化問題並將輸出保存到臨時文件而不是嘗試一次性管道來解決問題。

michael@DESKTOP-OI3AOU6:~$ ~/anaconda3/bin/jupyter lab ~ 2> >(tee /tmp/jlab ) & sleep 4 ; cat /tmp/jlab | grep ^[[:blank:]].*http.* | tr -d " \\t\\n\\r" | xargs firefox.exe -P jupyterlab ; rm /tmp/jlab; %

逐行瀏覽每個部分以供其他人參考:


~/anaconda3/bin/jupyter lab ~ 在主目錄(〜)中運行jupyter實驗室會話。


2> 將標准錯誤傳遞到文件后面。


>() 允許進入文件的管道被引導到封閉的命令的標准輸入中。

tee /tmp/jlab 將輸入重定向到臨時文件jlab並將其復制到標准輸出。 這就是我如何保留原始程序在終端中顯示信息的行為。 更多信息https://en.wikipedia.org/wiki/Tee_(command)

>(tee /tmp/jlab ) 輸出通過管道傳輸到tee命令


& 允許進程繼續在后台運行。


sleep 4 等待4秒讓服務器旋轉起來。


; 命令執行下一個命令后


cat /tmp/jlab 將臨時文件/ tmp / jlab的內容安排到標准輸出中。


| 將程序的標准輸出左側輸入到右側的標准輸入。 在這種情況下cat /tmp/jlab進入grep ^[[:blank:]].*http.*


grep ^[[:blank:]].*http.* 提取開頭有空格的行,並在行中包含http。 它將允許在之間和之后的任何數量的charachters。 在這種情況下,它運行得非常好,但如果偶然更新jupyter更改輸出,這將是它將破壞的位置,將選擇更合適的正則表達式。


| 輸出grep管道到tr


tr -d " \\t\\n\\r" 從行中刪除所有制表符空格和換行符。


| 將tr的輸出管道輸出到xargs。 這是jupyter會話獨有的完整URL。


xargs firefox.exe -P jupyterlab Xargs獲取其標准輸入並將其作為參數提供給以下命令。


在這種情況下, firefox.exe是一個軟鏈接,我存儲在/usr/local/bin/firefox.exe ,軟鏈接指向已安裝的Windows位置/mnt/c/ ,它位於/mnt/c/Program Files/Mozilla Firefox/Firefox.exe 我以這種方式安裝它的原因只是我的慣例,因為Windows可執行文件比WSL中執行的程序具有更好的渲染並且通過xming運行。

-P jupyterlab 啟動我制作的配置文件,從firefox中刪除選項卡和導航欄。 我還訪問了firefox中的自定義選項,以便顯示標題欄。

通過在特定配置文件目錄%APPDATA%\\Mozilla\\Firefox\\Profiles\\設置自定義css來設置%APPDATA%\\Mozilla\\Firefox\\Profiles\\

該文件的完整路徑是%APPDATA%\\Mozilla\\Firefox\\Profiles\\8vv7gs2r.jupyterlab\\chrome\\userChrome.css

這個文件將設置firefox,因此它沒有選項卡或導航使窗口混亂。

該文件的內容如下:

/*
 * Do not remove the @namespace line -- it's required for correct functioning
 */
@namespace url("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"); /* set default namespace to XUL */

/*
 * Hide tab bar, navigation bar and scrollbars
 * !important may be added to force override, but not necessary
 * #content is not necessary to hide scroll bars
 */
#TabsToolbar {visibility: collapse;}
#navigator-toolbox {visibility: collapse;}

; 命令執行下一個命令后


rm /tmp/jlab 拆除臨時文件的鍋爐板線。 它通常應該在重置linux系統時刪除,但它並不適用於所有實現。 我沒有檢查wsl是否做到了。


; 命令執行下一個命令后


% 將帶有&的后台移動的最后一個作業移動到前台。 現在執行的程序現在會像移動到后台之前那樣執行中斷。

你可以很容易地做到這樣的伎倆:

$ echo "hello world" | { read test && sleep 2 && echo $test; } | xargs echo

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM