I need a .bat file that will close and re-open start.cmd (C:\Users\Jake\Desktop\PocketMine-MP\start.cmd) <-- that's the file location. I need it to close and re-open every 75 min. The terminal has to close it can't stay open and launch another all I've gotten so far is:
#echo off
:loop
start "start.cmd" "C:\Users\Jake\Desktop\PocketMine-MP\start.cmd
timeout /t 20
taskkill /f /im "start.cmd" >nul
goto loop
Its starting the terminal every 20 seconds like I want it too but its not closing the old one.
If anyone can help it would assist me and my small network greatly.
Rename start.cmd to bat_start.cmd then try the following.
#echo off
:loop
start "bat_start.cmd" "C:\Users\Jake\Desktop\PocketMine-MP\bat_start.cmd"
timeout /t 20
taskkill /f /fi "windowtitle eq bat_start.cmd*" /im "cmd.exe" >nul
goto loop
If you use same file for control the loop and start file again, then it's fall in recursion. Following example work for me,
#echo off
:loop
start "test2" "C:\test2.bat"
timeout /t 60
taskkill /f /fi "windowtitle eq test2*" /im "cmd.exe" >nul
goto loop
test2 file
#echo off
echo "start job here"
:: Do your work here
pause :: remove the pause, it's just for simulating
exit
You can use the TimeCommander plugin. Set to run a restart command every 75 minutes.
Related
In short, I need to start 1.bat, that can CALL or START 2.bat to execute TASKKILL /im explorer.exe /f /t but keeps "1.bat" running (or reopens it) once "2.bat" is finished. The difficulty is, I need to keep the /t switch in TASKKILL to make it transferable.
Full Explanation:
I have several older laptops running Windows 7 x64. They will run games, but only if I use Task Manager to end Explorer, it's associated processes and stop several unneeded services. This frees up RAM and CPU to start games via Task Manager..
To avoid ending each process/service individually every time, I wrote 2 cmd batch files:
1) Options.bat -- (SHORTENED)
#echo off
:begin
echo (0) Kill Processes
echo (1) Run [game]
SET /p op=Select Task:
if "%op%"=="0" goto killall
if "%op%"=="1" goto op1
killall:
CALL "C:\TK.bat"
cls
goto begin
op1:
start " " /realtime "C:\[exe path]"
goto exit
:exit
exit
2) TK.bat -- (SHORTENED)
net stop [service]
taskkill /im explorer.exe /f /t
taskkill /im [specific process].exe /f /t
Both of which work as intended - provided I initially start Options.bat via Task Manager.
Problem: when I run Options.bat from Windows Explorer (even "Run As Administrator") and call the TK.bat script to run TASKKILL /im explorer.exe /f /t it does work, but also closes the CMD window, when i want it to return to the options selection. This does make sense when i'm using the "tree" attribute for TASKKILL.
However, if I run Options.bat via Task Manager, and CALL TK.bat, it will execute the commands (without closing itself) then return to the task selection - which is exactly what i want to happen!
I assume this is because it is running as the Local System account via Task Manager and not mine or the built in Administrator account?
I have tried the RUNAS command within Options.bat -
RUNAS /user:Adminstrator "C:\TK.bat"
which runs TK.bat, executes the TASKKILL command and then RUNAS again to return to the first batch file in my username. This does work, but I still have to press enter at each password prompt.
I have also tried numerous variations of the TASKKILL /FI switch:
TASKKILL /fi IMAGENAME ne cmd.exe /im explorer.exe /f /t`
TASKKILL /fi USERNAME eq [name] /fi WINDOWTITLE ne Options.bat /im explorer.exe /f /t
Which, I thought, would end all processes "not equal" to cmd.exe/Options.bat but I cannot get it to work.
Question: Is there a way of executing the TASKKILL /im explorer.exe /f /t within TK.bat, that will not close the currently running batch file and without having to run it from Task Manager? Perhaps a different command or giving the Options.bat some sort of elevated authority to stop it from being closed when Explorer.exe /t is ended. Ideally, without installing separate 3rd party tools?
I know I could edit the batch file to end explorer.exe, end each associated .exe and then stop the services I do not need, individually, but this is time consuming and defeats the point of writing the file to make it automated and transferable (if needed).
Any help would be greatly appreciated and I can upload the full code of both batch files, if that would help.
Strangely enough, the simple taskkill /F /IM explorer.exe & start explorer command does this task. I don't know why, but it works.
To prevent taskkill /im explorer.exe /f /t from killing the cmd instance which is executing your batch file, You need to break the parent-child relationship between that particular instance of cmd.exe and explorer.exe.
The trick is to use two extra instances of nested cmds. The first one launches the second one and terminates immediately so the second instance becomes an orphaned process which can not be determined as descendant of exeplorer.exe. At this point the second instance can safely execute taskkill /im explorer.exe /f /t
start "Chain breaker" /min cmd /d /c start "Orphaned Process" cmd /d /c Options.bat
The above can be used from Command Prompt or from another batch file.
You can also incorporate this technique directly in to the Options.bat file, But extra logic is needed for Options.bat to determine when to launch itself in nested cmd and when to execute the actual code.
A sample script demonstrating the concept would be:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "tokens=3 delims=:" %%L in ("%~0") do goto %%L
REM Reinvoke the batch file forcing it to jump to the :main label
start "Chain breaker" /min cmd /d /c start "Orphaned Process" cmd /d /c #"%~d0\:main:\..%~pnx0"
exit /b
:main
taskkill /im explorer.exe /f /t
echo I'm alive
pause
exit /b
I am unable to call a bat process within another bat.
This is the situation.
I have a n1.bat that basically contains:
TITLE "n1.bat"
...
...
start /b /MIN "n2.bat"
...
The n2.bat contains:
TITLE "n2.bat"
...
...
start someother.bat
start another.bat
exit
Now I use a third bat (n3.bat) that must kill everything.
TITLE "n3.bat"
...
TASKKILL /F /T /PID n1_PID
...
exit 0
Unfortunately when n3.bat ends a window called n1.bat - n2.bat remains active (the echo output belongs to n2.bat).
I tried to kill it in some ways:
1- Tried to get the process pid from tasklist -> there is no process called n2.bat or similar
TASKLIST /V /NH>Tasks.txt
FOR /F "tokens=2 delims= " %%n2_pid IN ('FINDSTR n2.bat Tasks.txt') DO SET PID=%%n2_pid
TASKKILL /PID %n2_pid%
2- Trying to use TASKKILL /F /FI "WINDOWTITLE eq n2.bat" -> no process found
I tried the previous solutions also starting n2.bat with
start /min "n2.bat" (so without /b)
with no success.
The only way I manage to kill it is to calling
TASKKILL /F /IM cmd.exe
that I really would like to avoid since it obviously kills all opened cmd.
Any ideas to retrieve the guilty pid?
I have a batch file script which starts a program (bat) and after X seconds the batch script is close the program (exe) and restart it.
This is my code:
:loop
start "1.bat" "C:\1\1.bat"
Timeout 10
taskkill /f /im program.exe
goto loop
If the program is ending, I want that the Cmd.exe window closes, but only the "1.bat" window should be closed. I can't find a specified cmd window for my "1.bat" process in the task manager to close it.
If I close the programm.exe with && exit the batch doesn't restart!
You can do one of two things.
1 - If you have access to 1.bat (i.e. its not readonly or is not actually an .exe), then add exit after it runs the program.exe.
2 - Otherwise if you cannot modify 1.bat for whatever reason, you may need to add the following command line:
taskkill /FI "IMAGENAME eq cmd.exe" /FI "WINDOWTITLE eq 1.bat"
So your batch script will look as follows:
:loop
start "1.bat" "C:\1\1.bat"
Timeout 10
taskkill /f /im program.exe
taskkill /FI "IMAGENAME eq cmd.exe" /FI "WINDOWTITLE eq 1.bat"
goto loop
Or similar. Basically /FI is the filter switch and you can specify to filter based on the "IMAGENAME" (which equals "cmd.exe") and "WINDOWTITLE" (which equals "1.bat")
I have a X.exe program that takes about 2-6 hours to finish. Exact time is unknown, but I'd like to implement a threshold of 6.5 or 7 hours. If this program does not return any value by this amount of time, it will be killed. How do I implement this using batch *.bat files?
Here is what I had so far: a timer bat1.bat and an actual bat2.bat.
bat1.bat:
start cmd /C bat2.bat & timeout /t 25200 & taskkill /im X.exe /f
bat2.bat:
cd blah
bat1.bat
The problem with this approach is that only after 25200 seconds (or 7 hours) the timer will be stopped, and it won't be terminated before that limit. How do I tell the computer that if the program X.exe is finished then don't wait anymore?
Any help is appreciated!
I think this is a much simpler solution:
rem Start the process that will kill the X.exe program after 7 hours
start "WaitingToKill" cmd /C timeout /t 25200 ^& taskkill /im X.exe /f
rem Run the X.exe program and wait for it to terminate
X.exe
rem Kill the killer process and terminate
taskkill /fi "WINDOWTITLE eq WaitingToKill" /f
In this method there is not any additional code running at same time; just the waiting state of timeout command.
EDIT: Some explanations added
Note that both the "WaitingToKill" cmd.exe process with the timeout command and the X.exe program are running in parallel. If the timeout command ends after 7 hours, the taskkill /im X.exe /f command is executed, the X.exe program is killed and both cmd.exe processes ends.
If the X.exe program ends before the 7 hours, the Batch file execute the next line as usual. This line is taskkill /fi "WINDOWTITLE eq WaitingToKill" /f, so the window with the timeout command is killed and both cmd.exe processes ends.
thanks to #Squashman i was able to build a script on my own. seem to work fine
#echo off
setlocal enableextensions enabledelayedexpansion
set /a "checktime=60"
set /a "elapsedtime=0"
set /a "killtime=150"
set XProg=X.exe
start cmd /C runTest.bat
timeout /t 10
:while1
echo Go to WHILE loop.
echo elapsedtime = %elapsedtime%
echo killtime = %killtime%
set /a "timeleft = %killtime% - %elapsedtime%"
echo timeleft = %timeleft%
if /i %timeleft% geq 0 (
tasklist /fi "imagename eq %XProg%" 2>NUL | find /i /n "%XProg%">NUL
if "%ERRORLEVEL%"=="0" (
echo %XProg% is still running...
) else (
echo %XProg% is finished before timer.
)
set /a "elapsedtime = elapsedtime + checktime"
timeout /t %checktime%
goto :while1
) else (
taskkill /im %XProg% /f
echo %XProg% is terminated.
)
lessons learned:
1. hard to compare numeric variables in batch (compare diff with 0 instead)
2. terminated first time elaspedtime > killtime (might be a bit longer than killtime depending how often it checks)
I've tried various solutions but in the end this is really cumbersome in batch.
If you are willing to use an external tool the simplest way is
"C:\Program Files\Git\usr\bin\timeout.exe" 5 .\test.exe
It properly returns the exit code of the test process, also works if you spawn multiple test processes simultaneously and does not pop up windows all the time.
Here's my script in my bat file:
cd "C:\Program Files (x86)\Steam\Steamapps\common\F13game\"
Start F13theGameRemap.exe
cd "C:\Program Files (x86)\Steam\steamapps\common\F13Game\SummerCamp\Binaries\Win64\"
Start /WAIT SummerCamp.exe
Start /WAIT Taskkill /f /im F13theGameRemap.exe
exit
All I want is to run the first program, then the 2nd. When the 2nd program "summercamp.exe" exits, i want the first program "F13thegameremap.exe" to exit.
The way this script works now is that Taskkill immediately ignores the start /wait command of summercamp.exe and closes F13theGameRemap.exe immediately.
How do I fix this?
Something like this:
#Echo off
Cd /D "C:\Program Files (x86)\Steam\Steamapps\common\F13game\"
Start F13theGameRemap.exe
Cd /D "C:\Program Files (x86)\Steam\steamapps\common\F13Game\SummerCamp\Binaries\Win64\"
Set "App2=SummerCamp.exe"
Start /WAIT %App2%
:loop
Timeout /t 1 >NUL
tasklist.exe /FI "ImageName eq %App2%" /NH |find /i "%App2%" >NUL && Goto :loop
Start /WAIT Taskkill /f /im F13theGameRemap.exe
The :loop will check with 1 second delay if %App2% aka SummerCamp.exe is still running.