2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

シェルスクリプトを素早く再実行しても、一定時間は再実行されないようにする方法

Last updated at Posted at 2024-10-31

はじめに

シェルの設定ファイルの時間がかかる初期化処理を、シェルを起動するたびにやらないで欲しいと思って作ったものです。

実装コード

個人的趣味で、POSIX で標準化された範囲の機能だけで実装しています。

: > current.time
if [ ! -e next.time ] || find current.time -newer next.time | grep -q .; then

  # ここに処理を書く
  echo "do something"

  touch -d "$(TZ=UTC-00:05 date +%Y-%m-%dT%H:%M:%SZ)" next.time
fi

こちらは POSIX.1-2024 で新たに標準化された [ ... -nt ... ] を使って実装したコードです。-nt はほとんど(すべて?)の POSIX シェルで実装済みのはずです。

: > current.time
if [ ! -e next.time ] || [ current.time -nt next.time ]; then

  # ここに処理を書く
  echo "do something"

  touch -d "$(TZ=UTC-00:05 date +%Y-%m-%dT%H:%M:%SZ)" next.time
fi

簡潔に書けたと思いますが、どうやらこの方法では 24 時間までしか指定できないようです。それ以上のことをやる場合は、以下を併用かな?

追記 ああ、いや、ファイルのタイムスタンプを使う代わりに、Unixタイムをファイルに保存して比較したほうが簡単そうですね。

さいごに

あんまりテストはしていません。

以上

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?