Crontab에서 job 중복 실행 방지

2024. 12. 24. 21:52배치

옵션은 없음

Lock 파일 사용

  • flock - Ensures only one instance of the job can run at a time.
  • -n - Fails immediately if the lock cannot be acquired.
  • /tmp/myjob.lock - Lock file location.
  • /path/to/script.sh - The script to execute.

 

* * * * * flock -n /tmp/myjob.lock /path/to/script.sh

 

PID 체크

  • Creates a PID file (/tmp/myjob.pid) to track the running job.
  • Exits if the file already exists.
  • Deletes the PID file after the job completes.

 

* * * * * [ -e /tmp/myjob.pid ] && exit 1 || echo $$ > /tmp/myjob.pid; /path/to/script.sh; rm -f /tmp/myjob.pid

 

pgrep 사용

  • pgrep -f - Searches for processes by command name.
  • Runs the script only if no matching process is found.

 

* * * * * pgrep -f "script.sh" > /dev/null || /path/to/script.sh

 

'배치' 카테고리의 다른 글

Quartz 같은 job이 중복되게 작동하지 못하도록 하기  (0) 2024.12.24