Running multiple instances of a Batch file in windows simultaneously? - windows

I have a windows batch file that is invoked by windows scheduler. When I try to have multiple windows scheduler tasks trying to run the batch file simultaneously, the batch file is locked by the first process and the all the other instances fail.
Is there is way in Windows to run multiple instances of batch file simultaneously?
My script is a simple one all it does is:
set java_classpath
java javaClass

There is nothing inherent to batch file mechanics that limits the number of processes that can simultaneously run the same script. The actual batch script is not locked when it is run. In fact, it is possible to modify a batch script while it is running, though that is usually a very bad idea.
But a batch script could take any number of actions that would prevent simultaneous runs. The most obvious is if the script attempts to redirect output to a specific file (constant path and name). The output redirection establishes an exclusive lock that will prevent any other process from obtaining the same lock.
Another possibility is your script could be calling an external command or program that establishes an exclusive lock in some way.
Either way, there should be nothing to prevent multiple processes from launching the same script simultaneously. But if the script establishes an exclusive lock, then one (or more) of the instances may either crash or exit prematurely, or seem to hang, depending on how the failed lock aquisition is handled.
There really isn't any way to be more specific unless you post your actual script. But if it is a long script, then you should attempt to isolate where the problem is occurring before posting.

Windows 8 task scheduler has the following option (on the last, "Settings" tab):
If the task is already running, then the following rule applies:
Do not start a new instance (default)
Run a new instance in parallel
...
Probably you should change this setting. And also, I would suggest you look into http://serverfault.com and post there.

Did you try calling your batchfile by using %systemroot%\cmd.exe /K C:\path\batchfile.bat? With /K each time a new instance of cmd is opened, guess it is the shell not the file making you weird.

to people coming here from google simply looking for a way to run multiple instances of a .bat file simultaneously, a simple way would be this script:
set N=3
for /L %%i in (1,1,%N%) do (
start yourscript.bat
)

Related

Make a batch file type something in 2 consoles

I'm trying to make a batch file that types "quit" in two console windows. These console windows are hosting a game server each and I want them to shutdown when I use the said batch file.
I tried making a batch file terminating the server-console process but that might damage my server logs and such.
Is there a way for a batch script to type something into two separate cmd-windows and then simulating an 'enter'-push?

Run batch file in independent environment

I'm trying to run multiple instances of the same .bat file.
The batch file opens up other programs which when running multiple instances causes connection errors. Is it possible for me to run these batch files in separate "environments" where all code executed is run in way that it won't conflict with anything else?
No. Anything that can be done is being done.
Every program thinks it is the only program running, and it has access (on 32 bit) to 4 GByte of memory. The CPU does the above with a bit of help from windows.
Windows has device drivers that pretend to programs they are the only programs using it. But some things can't be shared, and programs should either wait or do something else if unavailable.
You may only have a timing problem. The command timeout /t 10 waits 10 seconds in a batch file.

How to restart program automatically if it crashes in Windows?

How can I start my program automatically if it crashes on windows 2003 server? Sometimes my program just crashes, is there a way in windows or settings that I can set?
There are several ways to create a process supervisor/guardian process on Windows.
First, is to leverage windows command line capabilities. Create a bat file:
#echo off
:start
start /w "your app to watch.exe"
goto start
start /w will wait for the process to exit. When the process crashes and exits, the bat script will relaunch it.
Another option is to use free supervisor tool https://github.com/chebum/Supervisor. It allows to restart the crashed app, plus it allows to monitor two or more apps at once and it will automatically close these apps when supervisor's window is closed.
The usual approach is to run what is known as a guardian process. This is a separate process, often a service, that monitors the state of the main process. When the guardian detects that the main service has died, it re-spawns it.
To the very best of my knowledge, there is not built in Windows functionality to do this for you.
Notice: running self-looping bat files can be useful, but unless you know what you're doing, they can wreak all kinds of havoc. This goes especially if you run them on startup. You have been warned.
Anyway. I just remembered something from my 286 days, when I played around a lot with BAT files. If you write the file
yourprogram.exe
some other event
the BAT file will run yourprogram, and then pause and wait around in the background until the program exits. After that it will run "some other event". This used to be kind of annoying if you wanted to run multiple things at once, but here it's actually useful. Using this, it's possible to make it run a loop that restarts the program (and reruns the bat file) as soon as it exits. Combine this with https://superuser.com/questions/62525/run-a-completly-hidden-batch-file, and you'll never even see it happening.
The final BAT file ("restart.bat" in this example) will look something like:
c:\[location]\yourprogram.exe
wscript "C:\[location]\invisible.vbs" "C:\[location]\restart.bat"
That's about it. Start the program (on startup via task or even just startup folder) with line 2, and this ought to solve your problem :)
Oh, if you want to stop the loop, just rename the bat file or put "// " in front of the two lines, save it, and exit the program.
If the program you are running requires admin rights, the solution I found was using psexec (http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx) to run both the program and the bat with elevated privileges. In that case the BAT will look like:
c:\[location]\psexec -h c:\[location]\yourprogram.exe
c:\[location]\psexec -h wscript "C:\[location]\invisible.vbs" "C:\[location]\restart.bat"
Then you run the bat as administrator, or run the second line (without the psexec part) from task scheduler with elevated privileges. BEWARE: running it as a normal user and clicking "no" on the UAC prompt gave me a BSOD, probably because it looped "can't run program because of lacking privileges" a couple of billion times or something :)
You can use RegisterApplicationRestart.
"If you register for restart and the application encounters an
unhandled exception or is not responsive, the user is offered the
opportunity to restart the application; the application is not
automatically restarted without the user's consent. "
For automatic restart without user intervention, there is also RestartOnCrash. Works with all Windows versions.
I was looking for something similar. There are two options to handle this - either you can write a small script by yourself or use something that is already existing.
After some googling I came across this nice list. The blogger has compiled about 8 tools to automatically restart a crashed or closed application.
Unfortunately there are no settings in Windows to automatically restart a regular program when it crashes.
Do you need to actively interact with your application's GUI? Some of the Service Wrappers (designed to run any application as a Windows Service) will monitor your application and restart it when it fails, but be sure investigate Session 0 Isolation to ensure that it won't get in the way.
You may use some special app like BDV SystemEvents or any other. It allows you to specify application which will be started if some another application is closed. Specify the same application as a Condition and as an Action and you will get expected results.

CMD.exe consumes all the CPU, blocking other CMD.exe to execute

We have some Batch scripts (.bat) in Windows to execute the “backups” and “archive log” for the databases. These scripts are called from Tivoli periodically.
For each executed script, the process creates a sub session in order to load the DB2cmd environment, in order to execute the db2 commands and exit.
daily.bat
call db2cmd hourly.cmd
The content of the script is this:
db2_job_saveddaily.cmd
db2 -fE:\DB2\scripts\tmp\db2_job_savedbhourly.db2 -zE:\DB2\scripts\tmp\db2_job_savedbhourly.log
exit
The content of the db2 file is (however, it is not important because it is executed correctly)
db2_job_saveddaily.db2
archive log for database ICMNLSDB
We are facing a problem with these scripts, and I think it is related with the exit. At one execution, the script freezes, and it starts to consume the whole CPU (see attached image). After this behavior, we cannot execute any other DB2 command from the CLP.
We kill all the CMD.exe and db2bp.exe processes, but the error persists.
There is nothing in the db2diag.log file, and the only solution is to restart the machine.
Probably, the CMD.exe process losses the communication with the db2bp.exe, and the exit cannot be executed. I would like to understand the origin of this problem and learn how to execute db2 processes in Windows.
Our friend #AngocA seems to check into SO often but hasn't been checking this dangling question even though he did something to close it. Let's at least put his answer in here so folk know it's CLOSED by user. :) Courtesy of tonight's Point Pimp. :-D
"The problem was in another db2cmd session where there was an
infinitive loop. This created a scenario when new db2cmd session
blocked because the first session used the whole CPU. – AngocA"

Scripting for safe file backup under windows

I need to back up some large files that are being written to disk by a process. The process is perpetually running, and occasionally dumps large files that need to be moved over the network. Having the process do this itself is not an option, as the process locks out users whilst it is doing file dumps.
So, this runs under a windows machine, and as a primarily linux user, I am not entirely certain how to do this...
Under linux I would simply use a cron job in the folder (I know the glob that will match the output files), then check lsof, to ensure that the file is not being written to, such that I don't try to copy a partially complete file. Data integrity is critical, so I would normally md5 the files before and after the copy.
So I guess my question is -- how does one do this sort of stuff under windows? I feel like I am kneecapped from the start -- I can use python, but I can't emulate lsof, nor cron to do the task scheduling.
I tried looking at "handle" -- but it needs admin privelidges at execution time, which is also not an option. I can't run the backup process as an admin, it has to run with user privs.
Thanks..
Edit: I just realised I could keep the python instance running, with a sleep, so task scheduling is not a problem :)
For replacing cron you can use the "Task Scheduler" in windows to start your script every few minutes (or specific times).
For lsof the question was discussed here : How can I determine whether a specific file is open in Windows?

Resources