Thursday, February 5, 2015

HOWTO Create lockfile logic using BATCH language

Overview

The goal of this HOWTO is to show how to manage a lock file in batch language. This is handy for scheduled processes.

Example

@echo off
REM
REM Example of howto use a lockfile in BATCH language
REM

:INIT
CD \BatchJobs
ECHO Current Working Directory
CD

:START
IF EXIST NIGHTLYRUN.LOCK GOTO SKIP_WORK
ECHO Turning on Lock
ECHO ""LOCK"" > NIGHTLYRUN.LOCK
REM Do work here. this is where your program or process goes. 
ECHO Turning off Lock
DEL NIGHTLYRUN.LOCK

GOTO DONE

:SKIP_WORK
ECHO ""This process is already running..""

:DONE

Conclusion

Using this approach you can prevent multiple instances of something running. A possible enhancement is something that can sense a stale lock if there is a crash or something. however this should help for most situations.

No comments: