Batch with "close cmd.exe and then re-execute" in timed loop - windows

I have a batch file to run an application (I used to write some basic code in the 80s in DOS), and I need it to "re-execute" the application every x hours. The thing I cannot find is how to "close" (or stop) the application before running the code again, so it wont keep opening several cmd.exe windows with every loop. I am a total noob at coding but I know the principles of batch files
My actual .bat looks like this
setx some-minor-adjustments
application.exe -couple-of-variables
Then, it opens the usual cmd.exe window, and keeps running the application forever. What I need to do is, somehow, "refresh" (close and re-execute) the application automatically every x hours.
Closing the cmd.exe window is just fine to close the application, nothing more is needed in that sense (like data to save for example)
A friend suggested the cron command but it seems to be for other purposes. I also find over the Internet that I should use the start command to open the application in a new cmd.exe window, or something like that
Any ideas? Thanks in advance!

On Windows, the Task Scheduler is used instead of CRON.
Tasks in task scheduler can be set to shut down after a set time if you can be sure of the maximum duration.
You can also add an exit command after the main command if the main command can be assumed to finish (or spawns to a separate process).

:BEGIN
start /b mspaint
ping -n 6 localhost
tskill mspaint
GOTO BEGIN
This should work on all versions of Windows. Just keep in mind that you have to change the number 6 to the number of seconds of delay you want plus one i.e., the above ping statement will produce a delay of 5 seconds. Similarly, to produce a delay of 1 hour, you will have to write
ping -n 3601 localhost
Also make sure that the batch file is in the same folder as your executable otherwise you will have to specify full path.
Note: You can use mspaint.exe in the start command but for the tskill command, parameter should be only mspaint(no extensions). Using #echo off won't be of much help in this case as the ping requests are echoed back and can't be turned off. You can instead use a batch-to-exe converter and create a silent windowless executable. The -n parameter of ping can be very large but has to be in seconds.

Related

Run Multiple .exe Files from a Batch File Simultaneously and Silently

I am on windows 10 and i need to run multiple executable files from a batch file silently, without waiting for them to finish. at the moment i have:
#echo off
start "" "%~dp0executable.exe" /q
start "" "%~dp0executable2.exe" /q
but this still opens multiple console windows.
any workarounds that achieve the same results are welcome.
Your executables seem to be console applications, otherwise no console window would appear.
Anyway, the start command features an option /B; here is an excerpt of the output of start /?:
B Start application without creating a new window. The
application has ^C handling ignored. Unless the application
enables ^C processing, ^Break is the only way to interrupt
the application.
By ^C and ^Break, pressing Ctrl + C and Ctrl + Pause/Break is meant, respectively.
In case you have a third .exe that needs to wait after the first two started simultaneously you can also use the start /w argument for the second one and call the third like so:
#echo off
start /B "%~dp0executable.exe"
start /W "%~dp0executable2.exe"
call "%~dp0executable3.exe"

Put server on heavy load for testing in Windows

Need to heavily exercise an eight-core server under Windows. Found a Linux exercising test at Put server on heavy load for testing and would like to perform a similar workout using command prompt commands.
Solved: I created a batch file DO.BAT which contains one line
dir /s /r /on & do ^z
and then from within a command prompt window entered
start do
which opened a new command prompt window and launched DO.BAT to loop until a Ctrl-Break is entered. Then, repeated that until many windows were running.
Many thanks to Bali C who mentioned the start command in How to run multiple DOS commands in parallel?

How to work around ebook-convert.exe (in calibre portable) halting batch file execution?

I use ebook-convert.exe from a batch file, and on one of my machines (old Windows vista laptop), any calls ebook-convert.exe prevent further commands in a batch file from running (in that .bat file, or in that cmd shell instance for instance). The conversion to .mobi actually succeeds, prints no errors even in -v -v full verbose mode. I tried versions 1.20.0 and 0.9.6 of calibre portable. It seems to be related to the executable itself, and not to the specific conversion job (just running it with -h flag has same effect). I tried running cmd in administrator mode, wrapping it with 'call' command, nothing helps. Assuming the calibre issue is a black hole that can't be solved, is there a DOS trick I could possibly try to keep batch commands running no matter what odd thing happens with ebook-convert.exe on that machine?
Using call in a batch file should work on normal console programs but it is possible for programs to "escape" if they really want to.
Using Start instead might work but it does not wait by default and its syntax is not sane:
Start /B /WAIT c:\path\without\spaces.exe optionalParameter
or
Start "" /B /WAIT "c:\path\w i t h\spaces.exe" optionalParameter
Remove /B to run the application in a new console.
Another option is to call ping localhost and For (to get the file size with %%~zA) in a loop until the size of the destination file stops increasing. This is a bit of a hack and might not work, it depends on how the converter opens the file and how often it flushes the writes.

Windows 8.1 batch file - closing separate command window

I'm trying to run a batch file that will close a different cmd window. I know I can do this:
TASKKILL /IM "myapplication.exe" /F
But whilst this does (obviously) kill the task, it doesn't close the cmd window myapplication.exe was running in. I cannot edit myapplication.exe or I would put an 'exit' on the last line.
Any help would be much appreciated.
Well, if your program was started from cmd, then obviously cmd is still running even if you kill or exit that application. To kill cmd as well you can apply the same command to a different program (in a quite obvious way).
However, this will also stop your batch file for equally obvious reasons.
Is there a particular need to run myapplication from cmd?

How do I get a windows batch file to pause while an executable runs?

I am writing a batch file, in which I call an EXE to execute. Now, statements after the call to the EXE should not execute till the EXE completes its execution. How can I do it in the batch file (on Windows)?
START /WAIT First.exe
START /WAIT Second.exe
It depends on how the .exe works. I'm afraid I don't have all the technical details or terminology, but some .exe files will return control of the session immediately after they've started, while others won't return control until after the program has terminated.
The second case is easy as the commands late in the file won't execute until the former have completed, so I'll assume you're facing case #1.
An easy workaround/hack if the execution takes (approximately) the same amount of time every time it runs is to use a ping command with a delay.
PING 127.0.0.1 -n 1 -w 120000 >NUL
This will force the ping command to run once with a 120000ms (2 min) delay.
There's also a good article on a more complex (but more reliable method) on fpschultze.de with a much more detailed explanation. In short you query the task list searching for the executable you're waiting for. As soon as it's not there you carry on with the batch file. It also uses the ping method, but in a different manner.
The statements in a batch file are executed sequentially. So if your batch file looks like:
first.exe
next.exe
Next is executed if first is completed.
You could use the "start" command with parameter /wait
use start /? on commandline for details.
Here is the very simple process. Just keep the following line to wait for 10 secs and then continue the other commands...
TIMEOUT /T 10
That's all you have to do. I hope you enjoy my technique.
You can use
PAUSE
In batch scripting, but I don't understand your question.

Resources