沒有人的文庫


  • 首頁

  • 歸檔

  • 標籤

Use flock to Ensure Only One Instance of a Shell Script Is Running

發表於 2017-02-16 |
1
2
3
4
5
6
7
8
#!/usr/bin/env bash
script=$(realpath "$0")
lock_file="${script%.*}.lock"
(
flock -n 9 || exit 1
sleep 10 && echo 'Done!'
) 9> "$lock_file" && rm "$lock_file"

References:

  • Unix manual flock(1)
# Bash # Shell Script
Use fcntl.flock to Ensure Only One Instance of a Python Script Is Running
Use GNU Utilities on macOS
Pei-Lun H.

Pei-Lun H.

7 文章
9 標籤
RSS
© 2017 Pei-Lun H.
由 Hexo 強力驅動
主題 - NexT.Muse