I need to execute a synchronizing application continuously with a batch file. What I want to do is something like:
while true
execute application
sleep 1 minute
end while
Can this be done with CMD?
Thanks in advance.
Using pure batch you could do this:
:LOOP
yourprogram.exe
ping 127.0.0.1 -n 61 >NUL
goto :LOOP
This means you don't have to install any other programs or create more scripts. It might not be pretty but it works! I have pinged 61 times as pinging the loopback seems to create a delayed second.
Download unixutils for win32. In here, you will find sleep.exe. Put it somewhere in your path. (frex, in c:\windows)
Then, you can build a batch file similar to this
echo off
:here
sleep 60s
echo Exec app
rem your app goes here
goto here
Related
I have a cmd file 'D:\ProgramFiles\test.cmd'.
I want to write a script which would execute this test.cmd in a loop n times, also, after each execution, I want a delay/timeout of 5 seconds.
How should I go about this?
The timeout command is not always available. For example when executing a batch file on a build server like Jenkins or when running on an older version of Windows. However, there is a way that will always work:
#ECHO OFF
FOR %%i IN (1,1,5) do (
CALL "D:\ProgramFiles\test.cmd"
ping 127.255.255.255 -n 1 -w 5000> nul
)
Here we are using the ping command. ping 127.255.255.255 -n 1 -w 5000> nul will ping the IP address 127.255.255.255. This address won't be reachable but -w 5000 will make the command "wait" for 5000ms = 5s for a response. The only limitation is that you can't go below 500 ms with this method. Any number below 500 will result in a delay of 500 ms.
You can use ping -n 6 localhost>nul as well. This will repeat the ping six times. Notice that you will need 6 repetitions to achieve a delay of 5 seconds as the first ping will be launched immediately so six pings mean 5 seconds delay. Don't fall victim to the famous "fencepost error"
For further information check this website.
a For loop is what you want. in this instance, we tell it to start counting by 1, steps of 1, end at 5, which means you will be running 5 loops. For more on the for run from cmd.exe for /?
Timeout, depends on your OS version. Older OS's does not have it, then perhaps look at the older version called sleep or alternatively use the command ping with a count of timeout you want, +1. timeout On some Windows versions you do not like the /t and therefore can be used without it. i.e timeout 5
#echo off
for %%i in (1,1,5) do (
echo We do something here..
timeout /t 5
)
To learn about Windows cmd/batch commands, and how to use them in a batch files you can open cmd and type help which will list loads of commands. each of them can then be run followed by the /? switch to get full help on each command.
Example (n = 200):
FOR /L %%A IN (1,1,200) DO (
D:/ProgramFiles/test.cmd
timeout /t 5
)
I am probably missing the right vocabulary to talk about this problem more succinctly so excuse me if I'm a little wordy here. Under Windows 10 I have a program that runs inside a CMD command prompt It's an executable called OpenSim and it has it's own extensive command set, including 'shutdown', which initiates a graceful termination of the processes therein, closes SQL connections etc, then finally closes the CMD command window. I also have a CMD .bat file that is activated by my UPS when the power goes down that will of course open it's own window, and then does some housekeeping before closing down the hardware. One thing I want the .bat file to do is to somehow insert a 'shutdown'command into the other window's process. Is that possible? If so, how? Please assume I am a total newbie at this and you won't go far wrong. Thank you.
EDIT It looks like creating a file to flag the closedown event taking place is the only (and I guess rather primitive) way to do this. So, building on what others have said in stackoverflow, I have the following now. When I run it to test it waits - it doesn't. It runs right through to the end, running 'shutdown', even though the UPSFLAG.TXT file does not exist. What's going wrong?
echo Waiting for UPS Power Down Signal.
echo =================================
#ECHO OFF
SET LookForFile="C:\Opensim OSGrid\UPSFLAG.TXT"
:CheckForFile
IF EXIST %LookForFile% GOTO FoundIt
REM If we get here, the file is not found.
REM Wait 10 seconds and then recheck.
REM If no delay is needed, comment/remove the timeout line.
TIMEOUT /T 10 >nul
GOTO CheckForFile
:FoundIt
ECHO Found: %LookForFile%
rem Tidy up
del "C:\Opensim OSGrid\UPSFLAG.TXT"
shutdown
Adding double quote after the = will save your variable as that "C:\Opensim OSGrid\UPSFLAG.TXT" which you do not want. rather you want to store it as C:\Opensim OSGrid\UPSFLAG.TXT so move the quote to before lookforfile.
Also, you created a variable for the file, so you might as well use it in the delete.
Finally, as a safety measure, always put an exit after a goto. That will ensure the system exists should there be a problem in the script and you can make sure you do not delete files or shutdown the system when it was not planned for.
echo Waiting for UPS Power Down Signal.
echo =================================
#ECHO OFF
SET "LookForFile=C:\Opensim OSGrid\UPSFLAG.TXT"
:CheckForFile
IF EXIST "%LookForFile%" GOTO FoundIt
REM If we get here, the file is not found.
REM Wait 10 seconds and then recheck.
REM If no delay is needed, comment/remove the timeout line.
TIMEOUT /T 10 >nul
GOTO CheckForFile
exit
:FoundIt
ECHO Found: %LookForFile%
rem Tidy up
del "%LookForFile"
shutdown
I am trying to create a batch file that will reopen my KnockKnockClient and KnockKnockServer programs , get them started.
So far , my batch file looks like this :
#echo off
setlocal
start cmd
start java KnockKnockServer
ping 192.0.2.2 -n 1 -w 5000 > null
start /wait java KnockKnockClient
if errorlevel 1 goto retry
echo Finished successfully
exit
:retry
Also, suppose that I want it to restart so that the CMD window with the KnockKnockClient is active. Is that possible ?
any tips appreciate, thank
"start" itself invokes cmd.
you don't need to give "start cmd"
Maybe your directory path should be varied. (locate correct path of your program)
I have a batch script that starts up a process whenever it detects that the process was closed. I need to force close the process somehow on a set interval loop to see if the batch script is working as it should. What would be the best way to do this? I tried using another batch script to close the process, but didn't get the result as expected since both batch scripts open in the same command window.
In a second script, loop with goto and use a ping for pause.
:loop
:: 301 = 5 minutes. The first ping response is instant.
ping -n 301 0.0.0.0 >NUL
taskkill /im "program.exe" /f
goto loop
I am currently an administrator on a private Minecraft server, though in this case the technical question lies outside the scope of typical minecraft supoort.
I wish to have the batch file that launches the server restart at 12 am and 12 pm, though I have little experience in batch and a cursory google search brings up nothing helpful.
The issue I run into is both that I have no idea if batch CAN execute commands within a java server console, send the commands to save the server and then exit, and restart itself, due to only knowing basic batch functions.
More specifically, I want the batch file itself to run a command in the server window after either 43200 seconds or on each of the 12s, then restart itself. I do not know how to get a batch file to run a command within the server command line, or if it's even possible.
The current batch code is as follows:
#echo off
:Minecraft
echo (%time%) Minecraft started.
java -Xms2048m -Xmx2048m -XX:PermSize=128m -jar FTBServer-1.6.4-965.jar nogui
pause
echo (%time%) WARNING: Minecraft closed or crashed, restarting.
ping 1.1.1.1 -n 1 -w 3000 >nul
goto Minecraft
Any help would be aprreciated. Thanks.
i use this but if you want it to restart it automatically then just delete the :choise part and make a loop from start to restart
#echo off
title minecraft-server-1.8.3
color 0A
prompt [server]:
cls
:start
echo loading server...
java -Xms3G -Xmx3G -jar minecraft_server.1.8.3.jar nogui
cls
:choice
set /P a=do you want to restart[Y/N]?
if /I "%a%" EQU "Y" goto :restart
if /I "%a%" EQU "N" goto :stop
goto :choice
:restart
cls
echo server will restart
TIMEOUT /T 5
cls
goto :start
:stop
cls
echo closing server
TIMEOUT /T 5
exit
ps. replace minecraft_server.1.8.3.jar with the name of your server file
Solution 1:
I would suggest to use the windows task scheduler instead of a batch file. There you can create a task, schedule it to be triggered at 12am/pm and insert any cmd command you want to be executed. However, it's non-trivial to cummunicate with the server console without knowing the specific interface or how to administrate a minecraft server. What you can do is simply kill the server and restart it using the command line.
Solution 2:
If you don't like this solution and don't know how to communicate with the server console you can try this:
Take a look at AutoIt (https://www.autoitscript.com/site/). It's a VERY simple script language which also can simulate click and input from the keyboard. So you can write a script that sets the focus to your server console and types the desired command to restart the server. This AutoIt script can be compiled to an exe file or you can run it as an au3 script.
You should still use the task scheduler to run your exe/script at 12am/pm.
If you need some help writing the AutoIt script I can help you with that.
I wrote a similar program for a friend in AutoIt here is the script i commented the lines you need to config:
HotKeySet("{ESC}", end)
HotKeySet("{F1}", start) ;optional
HotKeySet("{F2}", pause) ;optional
pause() ; starts the pause loop when started
; restarts the server all 12 hours
Func start()
$Path = "PathToYourBatch.bat" ; self explained
While 1
If #HOUR = 00 Or #HOUR = 12 Then ;starts the server at 00 and 12
Run($Path)
EndIf
WEnd
EndFunc
Func pause()
While 1
Sleep(500) ; waits 500 ms to reduce lag
WEnd
EndFunc
Func end()
Exit
EndFunc
You dont need to use the hotkeys but you could easily control the program with them(remote desktop)
You can use a online compiler like (http://www.script-example.com/themen/AutoIT-Online-Compiler.php) or download it from (https://www.autoitscript.com/site/) hope i could help if any further questions with the code ask me.