Can I prevent more than one user from running a batch file at the same time

I have a batch file that can update a web project and clean / rebuild it. I made it executable for netizens. Now I want to make the executable file only by one user at a time. How to synchronize an object in programming languages.

Is it possible to do this?


source to share

1 answer

A simple solution to check if the batch file is running is to use the file system.

The batch file can check if the file exists and disallow execution in this case, otherwise it creates the file, runs commands, and finally deletes the file.

@echo off
if exist "C:\Temp\BatchLock.txt" goto BatchRunning
echo Batch file is running by %username%.>C:\Temp\BatchLock.txt

rem All other commands of the batch file

del C:\Temp\BatchLock.txt
goto :EOF

type C:\Temp\BatchLock.txt
echo Please run the batch later again.
echo Press any key to exit ...
pause >nul


Certainly C:\Temp

not a good place to store a lock text file. This should be a directory that is identical for all users, a directory on the server with write permissions for all users.



All Articles