PostgreSQLスキルアップノート(自己啓発のための個人サイト)
汎用一定間隔情報取得・bashコマンド編
【一覧に戻る】
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
■■■■ PostgreSQL スキルアップノート
■■■■
■◆■■ 汎用一定間隔情報取得・bashコマンド編
■■■■
■■■■
■■■■ 2013/01/03
■■■■ 使用環境:CentOS、Redhat (bash)にて確認
(C) 2013 ohdb
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
指定したコマンドを指定した間隔&回数分実行する。
トラブル調査など、その場で繰り返しOSコマンドを実行して情報を取得するような用途を想定。
(簡易的なものです)
■1■ 準備
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1回限りの使用の場合はそのままコマンドラインに貼り付ける。
これによりtimeloopファンクションが事実上コマンドとして利用できるようになる。
継続的に使用する場合には、ファンクションのくくりを取り除いてbashのシェルスクリプト
としてファイル名timeloopとしてパスの通った場所に保存しておく。
timeloop(){
if [ $# -eq 0 ] ; then
echo " Usage : timeloop interval count 'command'"
else
EXEC_COMMAND="$3"
for i in `seq $2`
do
echo "=================== $i / $2 $EXEC_COMMAND"
eval "${EXEC_COMMAND}"|awk '{print strftime("%Y/%m/%d %H:%M:%S") ,$0}'
if [ $i -lt $2 ]; then sleep $1 ;fi
done
fi
}
■2■ 実行方法
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
【□】 timeloop 実行間隔(s) 実行回数 '実行するコマンド'
・実行するコマンドは引数がある場合はシングルクォートやダブルクォートで囲む。
例)timeloop 1 10 'ps axur'
例)timeloop 1 10 'ps -ef | grep postgres'
・結果は本来のコマンドの出力に加えて、左端にタイムスタンプが付加される。
■3■ 実行例
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
【□】 timeloop 1 3 'free'
=================== 1 / 3 free
2013/02/24 12:18:58 total used free shared buffers cached
2013/02/24 12:18:58 Mem: 1399724 861408 538316 0 101984 542608
2013/02/24 12:18:58 -/+ buffers/cache: 216816 1182908
2013/02/24 12:18:58 Swap: 2818040 0 2818040
=================== 2 / 3 free
2013/02/24 12:18:59 total used free shared buffers cached
2013/02/24 12:18:59 Mem: 1399724 861408 538316 0 101984 542608
2013/02/24 12:18:59 -/+ buffers/cache: 216816 1182908
2013/02/24 12:18:59 Swap: 2818040 0 2818040
=================== 3 / 3 free
2013/02/24 12:19:00 total used free shared buffers cached
2013/02/24 12:19:00 Mem: 1399724 861408 538316 0 101984 542608
2013/02/24 12:19:00 -/+ buffers/cache: 216816 1182908
2013/02/24 12:19:00 Swap: 2818040 0 2818040
【□】 timeloop 1 3 'ps -ef | egrep "writer|autovacuum"'
=================== 1 / 3 ps -ef | egrep "writer|autovacuum"
2013/02/24 12:16:01 postgres 2147 2135 0 10:35 ? 00:00:00 postgres: writer process
2013/02/24 12:16:01 postgres 2148 2135 0 10:35 ? 00:00:00 postgres: wal writer process
2013/02/24 12:16:01 postgres 2149 2135 0 10:35 ? 00:00:00 postgres: autovacuum launcher process
2013/02/24 12:16:01 postgres 4087 4084 0 12:16 pts/0 00:00:00 egrep writer|autovacuum
=================== 2 / 3 ps -ef | egrep "writer|autovacuum"
2013/02/24 12:16:02 postgres 2147 2135 0 10:35 ? 00:00:00 postgres: writer process
2013/02/24 12:16:02 postgres 2148 2135 0 10:35 ? 00:00:00 postgres: wal writer process
2013/02/24 12:16:02 postgres 2149 2135 0 10:35 ? 00:00:00 postgres: autovacuum launcher process
2013/02/24 12:16:02 postgres 4092 4089 0 12:16 pts/0 00:00:00 egrep writer|autovacuum
=================== 3 / 3 ps -ef | egrep "writer|autovacuum"
2013/02/24 12:16:04 postgres 2147 2135 0 10:35 ? 00:00:00 postgres: writer process
2013/02/24 12:16:04 postgres 2148 2135 0 10:35 ? 00:00:00 postgres: wal writer process
2013/02/24 12:16:04 postgres 2149 2135 0 10:35 ? 00:00:00 postgres: autovacuum launcher process
2013/02/24 12:16:04 postgres 4097 4094 0 12:16 pts/0 00:00:00 egrep writer|autovacuum
以上