LoginSignup
1
0

More than 3 years have passed since last update.

[shellscript]一定周期ごとに同じコマンドを実行続ける

Last updated at Posted at 2020-07-19

この記事で紹介するもの

一定周期ごとに任意のコマンドを実行して、その実行結果を標準出力するスクリプトです。

kshで書いた為bashで動くかわかりません。
(たぶん動かない)

どんな場面で使う?私が使った具体例

・システム環境内でNTP(調時するためのデーモン)でDBサーバ、APサーバ等の時刻が同期されるのを目視確認するのに重宝しました。

コード

date_every_second.sh
#!/usr/bin/ksh

count=0

while true
do

echo '==============='
echo "DATE: `date`"

# このあたりで好きなコマンドを実行する

let "count=count + 1"
echo "count: ${count}"

sleep 1

done

・起動する時にパイプでteeコマンドを付けたらログ取りもできます。

標準出力をログ取りしたいときのコマンド
./date_every_second.sh | tee <ログ名>

以下はコマンドの出力例です。

出力例
$ ./date_every_second.sh
===============
DATE: 2020年  7月 20日 月曜日 02:50:19 JST
count: 1
===============
DATE: 2020年  7月 20日 月曜日 02:50:20 JST
count: 2
===============
DATE: 2020年  7月 20日 月曜日 02:50:21 JST
count: 3

ソースコードはこちらにもあります。
https://github.com/Massas/shellscript
github_profile#1.png


そのほか

GitHub: https://github.com/Massas
Twitter: kjm@DBサーバー https://twitter.com/kj53192539

1
0
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
1
0