0

I'm running Selenium using Maven through a .bat file from my desktop. Now, when I click for the first time, it runs. How can I avoid it to run when I click again? For example, it should show some message that an instance is already running.

This is my script:

cd C:\Automation\MavenHorizonAutomationFramework
mvn test 

3 Answers 3

3

Use something like:

REM Test for lock file
IF EXIST lock.txt (
REM Do nothing
) ELSE (
REM Create lock file
ECHO > lock.txt

REM Call your applications here

REM Delete the lock file afterwards
DEL lock.txt
)

Since you've already written a batch file, I'll assume you're capable of integrating this into yours.

If you want to enhance it then you should write the process ID to the lock.txt file and, if it exists, test if the process number in the file also exists before assuming that it's running. That way if the process dies and the lock file isn't removed, you won't get it incorrectly thinking it's still running the next time the batch file is run.

0
2

You can write a lock file next to the script and remove it at the end of the script. When you start the script you should show the message if the file exists and exit. If the file does not exist it can continue running.

This is how yum and apt are doing it.

1
  • Thanks for the replay can you please provide me the script and belos is my script cd C:\Automation\MavenHorizonAutomationFramework mvn test
    – ram
    Oct 17, 2013 at 8:29
1

Try using this at beginning of the .bat. It will check if another cmd.exe is already running and if so then it will exit.

set cmdcount="wmic process where name="cmd.exe" | find "cmd.exe" /c"
FOR /F "tokens=*" %%i IN (' %cmdcount% ') DO SET X=%%i

IF %x%==2 GOTO resume exit

:Resume cd C:\Automation\MavenHorizonAutomationFramework mvn test

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .