LoginSignup
41
50

More than 5 years have passed since last update.

tmuxを使いながらログを取る

Last updated at Posted at 2014-08-08

tmux で ウィンドウ単位のログが取得したかったのでメモ

何ができる?

tmux の ウィンドウ内の入力・出力を全て記録できます。(時刻情報付き!)

tmux_always_logging.png

動作確認環境

tmux 1.7 ※執筆している 20140808 時点の最新は 1.9a です。

※OS
  Ubuntu14.04
  Fedora14
 で動作を確認

.tmux.conf の編集

  1. ~/.tmux/log/ フォルダの作成
mkdir -p ~/.tmux/log/
  1. tmux.conf を編集(無い場合は新規作成)
 vi ~/.tmux.conf

以下を追加

# ===== logging =====
#
# C-b C-p Start logging.
bind-key C-p pipe-pane -o '/bin/sh -c "while read -r LINE; do echo \"[\$(date +\"%%Y_%%m%%d_%%H%%M%%S_%%N\")] \${LINE}\" >> \${HOME}/.tmux/log/\$(date +%Y%m%d-%H%M%S)-#S-#I.#P.log; done "' \; display-message "Logging start."
# C-b C-o Stop logging
bind-key C-o pipe-pane \; display-message "Logging end."
# C-b v Open new window. Logging always. (-n is set window name.)
bind-key v new-window -n 'logging...' \; pipe-pane -o '/bin/sh -c "while read -r LINE; do echo \"[\$(date +\"%%Y_%%m%%d_%%H%%M%%S_%%N\")] \${LINE}\" >> \${HOME}/.tmux/log/\$(date +%Y%m%d-%H%M%S)-#S-#I.#P.log; done "'
※#S セッションインデックス
 #I ウインドウインデックス
 #P ペインインデックス

使い方

新規ウィンドウを開く(常時ログ取得)

Ctrl+b, v の順で 「logging...」 という名前の新規ウィンドウが開きます。
開いたウィンドウは常に記録されています。

※ウィンドウ名が気に入らない場合は Ctrl+[,] で変更できます。

手動でのロギング

=== 開始 ===

Ctrl+b, Ctrl+p の順で

Logging start.

のような文字が最下部に表示され(一瞬!)、実際にログが出力されていれば OK です。

ログファイル例:
/home/user1/.tmux/log/2014_0808_113045-0-0.0.log

※CR などのエスケープ文字、制御文字等が含まれるので、ログファイルの内容の取扱には注意願います。

=== 停止 ===

Ctrl+B, Ctrl+o で停止します。
Logging end. とメッセージが表示されます。(一瞬)

【メモ】シェルスクリプト組込用のコマンド

これはただの私的なメモです。
ログを取得しながら tmux を開くコマンド


新規 tmux セッション開始時
tmux new-session \; pipe-pane 'cat >> LOGNAME'
新規 tmux セッション開始時(日付情報付)
tmux new-session \; pipe-pane -o '/bin/sh -c "while read -r LINE; do echo \"[\$(date +\"%%Y_%%m%%d_%%H%%M%%S_%%N\")] \${LINE}\" >> \${HOME}/.tmux/log/\$(date +%Y%m%d-%H%M%S)-#S-#I.#P.log; done "'

別ウィンドウを開く場合(既に tmux は起動済)
tmux new-window \; pipe-pane 'cat >> LOGNAME'
日付情報付
tmux new-window \; pipe-pane -o '/bin/sh -c "while read -r LINE; do echo \"[\$(date +\"%%Y_%%m%%d_%%H%%M%%S_%%N\")] \${LINE}\" >> \${HOME}/.tmux/log/\$(date +%Y%m%d-%H%M%S)-#S-#I.#P.log; done "'

【参考】tmux 1.7 のビルド

最新の tmux のリポジトリからビルド

git clone http://git.code.sf.net/p/tmux/tmux-code tmux-tmux-code
cd tmux-tmux-code

tag 1.7 を 1.7ブランチとしてチェックアウトする

git checkout refs/tags/1.7 -b 1.7

ビルドする

./autogen
./configure
make
sudo make install

ビルドできないときには・・・

aclocal が無い

apt-get install automake

cursesが無い

 sudo apt-get install libcurses-*


※ncurses とは違うの・・・?お兄さん教えて

libevent が無い

 sudo apt-get install libevent-dev

 又は

 sudo yum install libevent-devel.i686 -y
41
50
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
41
50