#author("2021-03-02T08:16:53+09:00","default:nobuoki","nobuoki") #author("2021-03-02T08:17:29+09:00","default:nobuoki","nobuoki") * はじめに [#p536ccae] coreutils の ts コマンドが使えないときに、それっぽい出力を得る方法です タイムスタンプを付与しつつバッファリングしないようにすると見た目が良いので awk を使います * 例1:エイリアス [#yd4785a4] コマンド出力結果をパイプで繋ぎます(coreutils の ts コマンドと似たような使い勝手です) #prism(bash){{{ # エイリアス登録(ダブルクオーテーションで括る例) $ alias ts="awk '{print strftime(\"[%F %T] \") \$0; fflush() }'" # 確認(シングルクォーテーションで登録するならこっち) $ alias ts alias ts='awk '\''{print strftime("[%F %T] ") $0; fflush() }'\''' }}} 出力例 #prism(bash){{{ # ls にタイムスタンプ $ ls | ts [2021-03-02 07:56:17] 1000202334 [2021-03-02 07:56:17] Desktop [2021-03-02 07:56:17] Downloads # ping にタイムスタンプ $ ping -c1 192.168.10.1 | ts [2021-03-02 07:56:31] PING 192.168.10.1 (192.168.10.1) 56(84) bytes of data. [2021-03-02 07:56:31] 64 bytes from 192.168.10.1: icmp_seq=1 ttl=63 time=2.96 ms [2021-03-02 07:56:31] [2021-03-02 07:56:31] --- 192.168.10.1 ping statistics --- [2021-03-02 07:56:31] 1 packets transmitted, 1 received, 0% packet loss, time 0ms [2021-03-02 07:56:31] rtt min/avg/max/mdev = 2.960/2.960/2.960/0.000 ms # sudo も大丈夫 # 最初の3行は標準エラー出力なのでタイムスタンプは付きません $ sudo apt update | ts WARNING: apt does not have a stable CLI interface. Use with caution in scripts. [2021-03-02 07:56:43] Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease [2021-03-02 07:56:43] Hit:2 http://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_20.04 InRelease [2021-03-02 07:56:43] Hit:3 http://security.ubuntu.com/ubuntu focal-security InRelease [2021-03-02 07:56:44] Hit:4 http://archive.ubuntu.com/ubuntu focal-updates InRelease [2021-03-02 07:56:44] Hit:5 http://archive.ubuntu.com/ubuntu focal-backports InRelease [2021-03-02 07:56:45] Reading package lists... [2021-03-02 07:56:45] Building dependency tree... [2021-03-02 07:56:45] Reading state information... [2021-03-02 07:56:45] 3 packages can be upgraded. Run 'apt list --upgradable' to see them. }}} 標準エラーにタイムスタンプを付けるのも簡単です #prism(bash){{{ # 標準エラーをリダイレクトしてタイムスタンプを付与 $ sudo apt update 2>&1 | ts [2021-03-02 08:06:52] [2021-03-02 08:06:52] WARNING: apt does not have a stable CLI interface. Use with caution in scripts. [2021-03-02 08:06:52] [2021-03-02 08:06:53] Hit:1 http://security.ubuntu.com/ubuntu focal-security InRelease [2021-03-02 08:06:53] Hit:2 http://archive.ubuntu.com/ubuntu focal InRelease (略) }}} * 案2:シェル関数 [#v5cd7188] * 例2:シェル関数 [#v5cd7188] time コマンドのように、コマンドを後置します #prism(bash){{{ ts(){ eval "$@" | awk '{print strftime("[%F %T] ") $0; fflush() }' } }}} 出力例 #prism(bash){{{ # 確認 $ type ts ts is a function ts () { eval "$@" | awk '{print strftime("[%F %T] ") $0; fflush() }' } # ls にタイムスタンプ $ ts ls [2021-03-02 07:58:42] 1000202334 [2021-03-02 07:58:42] Desktop [2021-03-02 07:58:42] Downloads # ping にタイムスタンプ $ ts ping -c1 192.168.10.1 [2021-03-02 07:58:50] PING 192.168.10.1 (192.168.10.1) 56(84) bytes of data. [2021-03-02 07:58:50] 64 bytes from 192.168.10.1: icmp_seq=1 ttl=63 time=3.90 ms [2021-03-02 07:58:50] [2021-03-02 07:58:50] --- 192.168.10.1 ping statistics --- [2021-03-02 07:58:50] 1 packets transmitted, 1 received, 0% packet loss, time 0ms [2021-03-02 07:58:50] rtt min/avg/max/mdev = 3.896/3.896/3.896/0.000 ms # sudo の例 $ ts sudo apt update WARNING: apt does not have a stable CLI interface. Use with caution in scripts. [2021-03-02 07:58:55] Hit:1 http://security.ubuntu.com/ubuntu focal-security InRelease [2021-03-02 07:58:55] Hit:2 http://archive.ubuntu.com/ubuntu focal InRelease [2021-03-02 07:58:55] Hit:3 http://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_20.04 InRelease (略) }}} * 参考 [#zf711b05] - [[プログラムを変更せずに標準出力ごとにタイムスタンプを付ける方法とPythonでのサンプル - 水底>https://amaya382.hatenablog.jp/entry/2019/05/18/182539]] - [[scriptコマンドで記録するターミナルログにタイムスタンプを付与する(ワンライナー編) | 俺的備忘録 〜なんかいろいろ〜>https://orebibou.com/ja/home/201702/20170207_003/]] - [[pingに日時(タイムスタンプ)をつける - Qiita>https://qiita.com/hachisukansw/items/727f7388b0f01ad1d631]]