LoginSignup
33
28

More than 5 years have passed since last update.

stdbufコマンドを使用してコマンドの標準出力にタイムスタンプを追加

Posted at

stdbufコマンドは、標準入出力ストリームのバッファ動作を変更してしてコマンドを実行することが可能。
http://linuxjm.sourceforge.jp/html/GNU_coreutils/man1/stdbuf.1.html

やりかた

command | stdbuf -oL gawk '{print strftime("[%a %b %e %H:%M:%S %Z %Y] "), $0; }'

commandの標準出力をうばって、行の先頭に時刻を追加しているだけです。

テストスクリプト

test.sh
echo "init"
sleep 1
echo "processing ..."
sleep 2
echo "done"

動作例

$ ./test.sh | stdbuf -oL gawk '{print strftime("[%a %b %e %H:%M:%S %Z %Y] "), $0; }'
[Sun Mar  9 16:04:06 UTC 2014]  init
[Sun Mar  9 16:04:07 UTC 2014]  processing ...
[Sun Mar  9 16:04:09 UTC 2014]  done
33
28
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
33
28