Created
September 19, 2023 05:04
-
-
Save kawaz/48a67c2edd6cfeb84d2847f296d3a69d to your computer and use it in GitHub Desktop.
シェルスクリプトでflockを使って2重起動防止をする方法(sh,bash,zsh,lock,)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# コピペで使えてシンプルなやつ (動的なファイルディスクリプタ) | |
# 2重起動防止 | |
exec {lock}<"$0"; flock -n "$lock" || { | |
echo "this script is already running" >&2 | |
exit 1 | |
} | |
echo "なにかの処理" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# コピペで使えてシンプルなやつ (固定のファイルディスクリプタを使う 3-9 が使える) | |
# 2重起動防止 | |
exec 9<"$0"; flock -n 9 || { | |
echo "this script is already running" >&2 | |
exit 1 | |
} | |
echo "なにかの処理" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# コピペで使えてシンプルなやつ (環境変数を使うタイプ、メッセージ無し) | |
# 2重起動防止 | |
[[ "$FLOCKER" = "$0" ]] || exec env FLOCKER="$0" flock -n "$0" "$0" "$@" | |
echo "なにかの処理" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment