I need to detect if a batch file is already running or not.
In the batch I simply loop and repeatedly do some tasks. Sometimes the loop cannot complete. I run the batch file at a scheduled time. When it runs, it should check if a previous instance exists and is healthy. If it doesn't exist it should start a new instance. If the instance that exist is not healthy/outdated, the new instance should kill/cancel the older process.
A few theories:
- How can I detect a previous instance in a batch file? Can I give an ID like thing?
- I can use a pulse mechanism to say the batch is healty; ie, a global variable, a newly created dummy file, a timestamp in a dummy file, etc.
- Can I detect a kind of "timeout", if the last pulse is expired then allow a new instance...
Yes, I all want them in a batch file - instead of a Windows app - I think it's not so hard for one who is a batch-file addict and takes this question as challenge!