watch
何するコマンド?
指定したコマンドを繰り返し実行するときに使うコマンド
書き方
watch [オプション] [実行するコマンド]
オプション
| オプション | 説明 |
|---|---|
| -d | 前回の実行結果と変わったところを強調表示してくれる Highlight the differences between successive updates. Option will read optional argument that changes highlight to be permanent, allowing to see what has changed at least once since first iteration. |
| --differences | 前回の実行結果と変わったところを強調表示してくれる Highlight the differences between successive updates. Option will read optional argument that changes highlight to be permanent, allowing to see what has changed at least once since first iteration. |
| -n【秒】 | コマンドの実行間隔を秒単位で指定する Specify update interval. The command will not allow quicker than 0.1 second interval, in which the smaller values are converted. |
| --interval【秒】 | コマンドの実行間隔を秒単位で指定する Specify update interval. The command will not allow quicker than 0.1 second interval, in which the smaller values are converted. |
| -p | うんちゃかんちゃら Make watch attempt to run command every interval seconds. Try it with ntptime and notice how the fractional seconds stays (nearly) the same, as opposed to normal mode where they continuously increase. |
| --precise | うんちゃかんちゃら Make watch attempt to run command every interval seconds. Try it with ntptime and notice how the fractional seconds stays (nearly) the same, as opposed to normal mode where they continuously increase. |
| -t | 1行目に表示される情報(ヘッダ部)を表示しない Turn off the header showing the interval, command, and current time at the top of the display, as well as the following blank line. |
| --no-title | 1行目に表示される情報(ヘッダ部)を表示しない Turn off the header showing the interval, command, and current time at the top of the display, as well as the following blank line. |
| -b | コマンドの実行でエラーが発生したらビープ音を鳴らす Beep if command has a non-zero exit. |
| --beep | コマンドの実行でエラーが発生したらビープ音を鳴らす Beep if command has a non-zero exit. |
| -e | コマンドの実行でエラーが発生したら、その時点で処理を止める Freeze updates on command error, and exit after a key press. |
| --errexit | コマンドの実行でエラーが発生したら、その時点で処理を止める Freeze updates on command error, and exit after a key press. |
| -g | 実行結果が変わったら処理を終了する Exit when the output of command changes. |
| --chgexit | 実行結果が変わったら処理を終了する Exit when the output of command changes. |
| -c | 色を変えてくれるっぽいけど、CentOS 7では変わりませんでした Interpret ANSI color and style sequences. |
| --color | 色を変えてくれるっぽいけど、CentOS 7では変わりませんでした Interpret ANSI color and style sequences. |
| -x | 「sh -c」ではなく「exec」に渡す command is given to sh -c which means that you may need to use extra quoting to get the desired effect. This with the --exec option, which passes the command to exec(2) instead. |
| --exec | 「sh -c」ではなく「exec」に渡す command is given to sh -c which means that you may need to use extra quoting to get the desired effect. This with the --exec option, which passes the command to exec(2) instead. |
| -h | ヘルプを表示する Display help text and exit. |
| --help | ヘルプを表示する Display help text and exit. |
| -v | バージョン情報を表示する Display version information and exit. |
| --version | バージョン情報を表示する Display version information and exit. |
補足事項
何もオプションを付けないで実行した場合、指定されたコマンドが2秒間隔で「sh -c」に渡されます。
例えば
watch ls
を実行した結果は
sh -c ls
sleep 2
clear
を何回も実行するのと、ほぼ同じです。
ちなみに「ls」コマンドは「ファイルやディレクトリの一覧を表示するときに使うコマンド」ね。
「sleep」コマンドは「処理を一定時間止めるときに使うコマンド」です。
「clear」コマンドは「画面の表示内容を消すときに使うコマンド」になります。
日本語にすると
1.ファイル一覧を表示
2.2秒間そのまま
3.画面の表示内容を消す
です。
「-n」オプションを指定すると、実行間隔を指定できます。
例えば
watch -n 1 ls
を実行すると「ls」コマンドが1秒間隔で実行されるようになります。
「-d」オプションを指定すると、前回実行との差分が強調表示されます。
例えば
watch -d ls
の実行結果が
Every 2.0s: ls Sat Jul 16 14:11:04 2016
hoge.txt
だったとします。
この状態で、新しいファイル「hoge2.txt」を作ると、実行結果が
Every 2.0s: ls Sat Jul 16 14:11:06 2016
hoge.txt
hoge2.txt
に なります。
「-d」オプションを指定しておくと、画面の表示内容が
Every 2.0s: ls Sat Jul 16 14:11:04 2016
hoge.txt
から
Every 2.0s: ls Sat Jul 16 14:11:06 2016
hoge.txt
hoge2.txt
に切り替わるタイミングで「hoge2.txt」が強調表示されます。
Every 2.0s: ls Sat Jul 16 14:11:06 2016
hoge.txt
hoge2.txt
「-t」オプションを指定すると1行目のヘッダ部が表示されなくなります。
watch ls
の実行結果が
Every 2.0s: ls Sat Jul 16 14:11:04 2016
hoge.txt
だとしたら
watch -t ls
の実行結果は
hoge.txt
に なります。
「-b」オプションはエラー発生時に音を鳴らすオプションです。
例えば「naiyo.txt」がない状態で
watch -b ls naiyo.txt
を実行すると、画面には
Every 2.0s: ls naiyo.txt Sat Jul 16 14:18:09 2016
ls: naiyo.txt にアクセスできません: そのようなファイルやディレクトリはありません
と表示され、コマンドが実行される度に「ビー!」と「ブー!」の中間くらいの音が鳴ります。
「-e」オプションはエラー発生時に処理を中断するオプションです。
例えば「naiyo.txt」がない状態で
watch -e ls naiyo.txt
を実行すると、画面には
Every 2.0s: ls naiyo.txt Sat Jul 16 14:18:09 2016
ls: naiyo.txt にアクセスできません: そのようなファイルやディレクトリはありません
と表示され、画面の下部に
command exit with a non-zero status, press a key to exit
と表示されます。
あとは何か適当なキーを押すと「watch」コマンドが終了します。
「-g」は、実行結果が変わったら「watch」コマンドを終了するオプションです。
例えば
watch -g ls
の実行結果が
Every 2.0s: ls Sat Jul 16 14:11:04 2016
hoge.txt
だったとします。
この状態で「hoge2.txt」を作ると「watch」コマンドが終了します。
「-x」オプションは「sh -c」ではなく「exec」にコマンドを渡すオプションです。
先ほど
watch ls
を実行した結果は
sh -c ls
sleep 2
clear
を何回も実行するのと、ほぼ同じと書きました。
watch -x ls
を実行した結果は
exec ls
sleep 2
clear
を何回も実行するのと、ほぼ同じになります。
「watch」コマンドの使い方は、そんなところでしょうか。
基本的には、オプションを指定しなくても実用に耐えると思います。
あとは必要に応じて必要なオプションを覚えていってください。
