sh/ネタ/コマンド出力にタイムスタンプ
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
* はじめに [#p536ccae]
coreutils の ts コマンドが使えないときに、それっぽい出力...
タイムスタンプを付与しつつバッファリングしないようにする...
* 例1:エイリアス [#yd4785a4]
コマンド出力結果をパイプで繋ぎます(coreutils の ts コマ...
#prism(bash){{{
# エイリアス登録(ダブルクオーテーションで括る例)
$ alias ts="awk '{print strftime(\"[%F %T] \") \$0; fflus...
# 確認(シングルクォーテーションで登録するならこっち)
$ 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...
[2021-03-02 07:56:31] 64 bytes from 192.168.10.1: icmp_se...
[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, ...
[2021-03-02 07:56:31] rtt min/avg/max/mdev = 2.960/2.960/...
# sudo も大丈夫
# 最初の3行は標準エラー出力なのでタイムスタンプは付き...
$ sudo apt update | ts
WARNING: apt does not have a stable CLI interface. Use wi...
[2021-03-02 07:56:43] Hit:1 http://archive.ubuntu.com/ubu...
[2021-03-02 07:56:43] Hit:2 http://download.opensuse.org/...
[2021-03-02 07:56:43] Hit:3 http://security.ubuntu.com/ub...
[2021-03-02 07:56:44] Hit:4 http://archive.ubuntu.com/ubu...
[2021-03-02 07:56:44] Hit:5 http://archive.ubuntu.com/ubu...
[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 'ap...
}}}
標準エラーにタイムスタンプを付けるのも簡単です
#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...
[2021-03-02 08:06:52]
[2021-03-02 08:06:53] Hit:1 http://security.ubuntu.com/ub...
[2021-03-02 08:06:53] Hit:2 http://archive.ubuntu.com/ubu...
(略)
}}}
* 例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; fflu...
}
# 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...
[2021-03-02 07:58:50] 64 bytes from 192.168.10.1: icmp_se...
[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, ...
[2021-03-02 07:58:50] rtt min/avg/max/mdev = 3.896/3.896/...
# sudo の例
$ ts sudo apt update
WARNING: apt does not have a stable CLI interface. Use wi...
[2021-03-02 07:58:55] Hit:1 http://security.ubuntu.com/ub...
[2021-03-02 07:58:55] Hit:2 http://archive.ubuntu.com/ubu...
[2021-03-02 07:58:55] Hit:3 http://download.opensuse.org/...
(略)
}}}
* 参考 [#zf711b05]
- [[プログラムを変更せずに標準出力ごとにタイムスタンプを...
- [[scriptコマンドで記録するターミナルログにタイムスタン...
- [[pingに日時(タイムスタンプ)をつける - Qiita>https://...
終了行:
* はじめに [#p536ccae]
coreutils の ts コマンドが使えないときに、それっぽい出力...
タイムスタンプを付与しつつバッファリングしないようにする...
* 例1:エイリアス [#yd4785a4]
コマンド出力結果をパイプで繋ぎます(coreutils の ts コマ...
#prism(bash){{{
# エイリアス登録(ダブルクオーテーションで括る例)
$ alias ts="awk '{print strftime(\"[%F %T] \") \$0; fflus...
# 確認(シングルクォーテーションで登録するならこっち)
$ 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...
[2021-03-02 07:56:31] 64 bytes from 192.168.10.1: icmp_se...
[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, ...
[2021-03-02 07:56:31] rtt min/avg/max/mdev = 2.960/2.960/...
# sudo も大丈夫
# 最初の3行は標準エラー出力なのでタイムスタンプは付き...
$ sudo apt update | ts
WARNING: apt does not have a stable CLI interface. Use wi...
[2021-03-02 07:56:43] Hit:1 http://archive.ubuntu.com/ubu...
[2021-03-02 07:56:43] Hit:2 http://download.opensuse.org/...
[2021-03-02 07:56:43] Hit:3 http://security.ubuntu.com/ub...
[2021-03-02 07:56:44] Hit:4 http://archive.ubuntu.com/ubu...
[2021-03-02 07:56:44] Hit:5 http://archive.ubuntu.com/ubu...
[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 'ap...
}}}
標準エラーにタイムスタンプを付けるのも簡単です
#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...
[2021-03-02 08:06:52]
[2021-03-02 08:06:53] Hit:1 http://security.ubuntu.com/ub...
[2021-03-02 08:06:53] Hit:2 http://archive.ubuntu.com/ubu...
(略)
}}}
* 例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; fflu...
}
# 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...
[2021-03-02 07:58:50] 64 bytes from 192.168.10.1: icmp_se...
[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, ...
[2021-03-02 07:58:50] rtt min/avg/max/mdev = 3.896/3.896/...
# sudo の例
$ ts sudo apt update
WARNING: apt does not have a stable CLI interface. Use wi...
[2021-03-02 07:58:55] Hit:1 http://security.ubuntu.com/ub...
[2021-03-02 07:58:55] Hit:2 http://archive.ubuntu.com/ubu...
[2021-03-02 07:58:55] Hit:3 http://download.opensuse.org/...
(略)
}}}
* 参考 [#zf711b05]
- [[プログラムを変更せずに標準出力ごとにタイムスタンプを...
- [[scriptコマンドで記録するターミナルログにタイムスタン...
- [[pingに日時(タイムスタンプ)をつける - Qiita>https://...
ページ名: