I believe a lock file is the simplest reliable solution. The trick is to make sure your running batch process maintains an exclusive write lock on the file until it terminates. The beauty of this system is that Windows will release the lock no matter what reason the batch terminates.
Once you have a lock file, you need a way to detect if the file is currently locked. I describe how to do this at How to check in command-line if a given file or directory is locked (used by any process)?
I've used this primitive, yet effective, technique to accomplish some fairly sophisticated tasks with Windows batch:
You are not clear where the batch file resides - on the remote machine, or the local machine, or if you might be running the script on multiple machines simultaneously, but only one active process per machine.
If the batch script is on the remote machine, and your process has write access to the script, then you can use the batch file itself as the lock file! You simply need to call a :subroutine while redirecting an unused file handle to the batch script using append mode. The CALL will fail if another process already has a lock. The lock will be released automatically when the script terminates (regardless of how it terminates).
myscript.bat
@echo off
:: Note - this extra call is to avoid a bug with %~f0 when the script
:: is executed with quotes around the script name.
call :getLock
exit /b
:getLock
:: The CALL will fail if another process already has a write lock on the script
call :main 9>>"%~f0"
exit /b
:main
:: Body of your script goes here. Only one process can ever get here
:: at a time. The lock will be released upon return from this routine,
:: or when the script terminates for any reason
exit /b
If your script is on a different machine than the process, but you only have the process running on one machine at a time, then I think the above will still work.
Perhaps a better alternative is to establish a dedicated lock file on each remote machine, separate from the batch script. Then you can run the process on as many remote machines as you want.
I have fillings that all you need to do is call something
Obs.: Remove extra space in set /A count[espace]+=1
and use @echo off
and replace %%~pa
to %%~dpa
@echo off
cd/d "E:\Temp\ziptest\Documents"
setlocal enabledelayedexpansion
for /r %%a in (*) do (
set /A "count+=1+0"
cd /d "%%~dpa"
set "fn=file!count!"
echo\!fn!
echo\!count!
7z.exe a -mx9 -mmt12 -sdel -pt3st -mhe=on "!fn!.7z" "%%~nxa"
)
endlocal
@echo off
cd /d "E:\Temp\ziptest\Documents" && setlocal enabledelayedexpansion
for /r %%a in (*)do set /A "count+=1+0" && cd /d "%%~dpa" && call set "fn=file!count!" && (
echo\!fn! & echo\!count! && call 7z.exe a -mx9 -mmt12 -sdel -pt3st! -mhe=on "!fn!.7z" "%%~nxa"
)
endlocal
Best Answer
I would pipe the output from
tasklist
intofind
, search formaplestory
, and use anIF
statement and aGOTO
to continue looping, checking for that process, until it's found. Once it's found, then you canGOTO
a different point to kill the task.Something like this should do the trick:
(The
TIMEOUT
command pauses the script for 1 second. This will help prevent it from sucking up too much CPU constantly running that loop.)