Batch file running under Task Scheduler continues running after batch file completes - batch-file

This is sort of a follow-up to my question earlier (link).
To test things out I made this simple batch file to ensure the Task Scheduler was properly executing the batch file:
cd "C:\Users\user\Desktop"
echo. 2>test.txt
So after the test.txt document is created on the desktop, the batch file should end but it continues to run:
Is there a way, either at the end of the batch file or a setting in the Task's Properties, to ensure that the cmd process quits?
Thanks!

I ran into the exact same problem. However, I felt duped when I read what Trevor778 wrote in this post:
I had the same problem - the task worked but the status kept showing Running. One simple thing to try is click on the Task Scheduler Library in the left column. Click Action/Refresh. Presto. Status changed to Ready. That's all it was for me, the task ran fine, just the status didn't update. Hope this helps.
ref: https://social.technet.microsoft.com/Forums/en-US/2f6dc29c-3b8b-45f5-a2a7-53e076acc062/task-scheduler-scheduler-status-is-being-running-always?forum=winservergen

you can add "exit" to last line of your script
cd "C:\Users\user\Desktop"
echo. 2>test.txt
exit

Running TASKKILL /F /IM cmd.exe will kill all cmd.exe processes whether it was the one that spawned this batch file or not. That's probably not desirable behavior. :-)
Judging by your last question, I'm guessing you're still running your task with cmd.exe /k, which will keep that window open indefinitely. For an unattended task, cmd.exe /c is a better choice. When the batch file finishes, the process should end.

Same here on Windows 7.
Putting all batch files in a directory in the user User specific path who runs the task
run programm = " cmd.exe " (without a path)
Your extras, mine where = " /c "C:\Users[username]\whatever\your_batchfile.bat" >> log.txt" "
" >> log.txt " so that i can see the output of the batch...
start in = " C:\Users[username]\whatever "
I also checked the "run with highest privilges" box
after that everything worked fine :)

Use following
exit /B
you may find more information in windows console area then type:exit/?

I know it's an old question, but I personally found that if I let a pause at the end of the bat file, it would keep the status as "Running".
I usually leave a pause at the end to help with debugging, but I found when I removed it, the task scheduler finally recognised it as having exited. It didn't help if I just refreshed it.

The solution I found was to add this line at the very end of the batch file:
TASKKILL /F /IM cmd.exe
Now after the batch file task runs and completes, it is no longer in the All Running Tasks list and the status goes back to 'Ready' instead of staying at 'Running'.
Warning:
That command will kill all running command processor instances so it may be potentially harmful!

Related

Run a different batch file when one coses

I have 2 batch files (1.bat and 2.bat). I want 2.bat to run when 1.bat closes. Is that possible?
Or is there a way to close a batch file when a batch file closes. Here is my code which doesnt work:
cd C:\xampp
start apache_start.bat
cd C:\Users\MinecraftServer\Desktop\1.12.2MinecraftServer
MC1.12.2Start.bat
taskkill /F /IM cmd.exe /T
I have a minecraft server running with a .bat file and a web server running off of a .bat file (using xampp, its used for prtg monitorng).
start will create a completely independent process and continue immediately to your next batch command. That independent process may do a job and terminate, or may say open an application like notepad and wait until the notepad is closed. Regardless, a straight start command will not stop the batch that it is in - the next step will be executed, whether or not the process that has been started has terminated.
You can also add the /wait switch to start. If this switch is used, the batch will wait until the started process terminates.
Preferred syntax for start is
start /wait "window title" executable parameters...
where "window title" may be an empty, but not absent string. Including this as the first parameter is preferred because the first quoted string in a start command is used as a window title for the started process, and thus start may not act as expected where the executable or any of its parameters is quoted.
call executablename... will wait for that executable to terminate before progressing to the next step. This may or may not be what is desired.
MC1.12.2Start.bat in your code will SWITCH to that batch and remaining batch lines will be ignored.
Which may be fortunate in this case, since as you've coded it, the apache_start.bat may or may not have completed when the taskkill command is run, killing all cmd.exe sessions.

kill a batch file that is stuck from another cmd box

Hope I'm in the right place here, wasn't sure where to post and this seemed the most likely
I use a cmd box to run a serial flasher program for repairing wifi boards, the way it works is this.
open a command prompt and change to the working directory
enter the command
sfh_DM36x.exe -nandflash -v -p "COM3"
this starts the flashing program, however, due to the way it works, the program when finishing the first time just loops with bootme bootme, this is normal behavior.
Normally I just hit CTRL+C and exit the batch file.
The same command is run again and this time the flashing process completes and you are returned to the C:\ prompt.
Now you have the information here is what I am trying to achieve.
Launch a batch file that runs the runflash.bat, kill the runflash.bat and then run runflash.bat again.
So far I have gotten as far as this (start.bat)
start c:\users\jud\desktop\runflash.bat
timeout /t 5
This runs a new CMD prompt and opens the runflash.bat
I've then entered a timeout of 5 seconds (long enough for the first attempt to finish)
at this point, I wanted to kill the currently open CMD prompt and then execute runflash.bat again.
at this point I'm stumped, any attempt to kill the second CMD prompt just kills the original, I've looked at taskkill but the PID changes each time I run the script, as well as other suggestions I've come across during google searches, most just relate to killing batch files manually or killing a CMD prompt where a batch file has finished.
Can this be done, the idea is to pass the file out to users who aren't familiar with CMD prompt workings as an all in one solution.
Provide the new process with a unique title (see start /? - the first quoted parameter is set as a title). Takskkill can then identify and close the process with its title:
start "JudRunFlash" "c:\users\jud\desktop\runflash.bat"
timeout /t 5 >nul
taskkill /fi "WindowTitle eq JudRunFlash"

Jenkins job stuck after command start

I have a problem in my jenkins job and I isolated into one command. So I created another separate job to try to fix it.
So in this job, called "teste" I only have one single command:
start cmd /k call "C:\Program Files\myDir\myBat.bat"
This opens a separate cmd window running my bat file, which should keep running "forever".
But the problem is when I do it, my jenkins job keeps stuck into a "exit 0" operation that I have no idea from where it came from.
Thats the console:
[EnvInject] - Loading node environment variables.
Building remotely on Machine01 in workspace C:\workspace\teste
[teste] $ cmd /c call C:\...dir\jenkins.bat
C:\workspace\teste>start cmd /k call "C:\Program Files\myDir\myBat.bat"
C:\workspace\teste>exit 0
Then it keep stuck at that point.
Example of myBat.bat content:
echo hi
pause
There's any way to make this call in another window without waiting for its finish?
I solve my problem changing the way I was calling my other .bat, calling it through powershell. But since I was from a bat file, I used the command to send a powershell command, calling my other bat file.
Also, I've added another line changing the jenkins BUILD_ID to a fake one, so it doesn't kill it.
So I changed from this line:
start cmd /k call "C:\Program Files\myDir\myBat.bat"
To this :
set BUILD_ID=dontKillMe
powershell -Command "Start-Process 'C:\Program Files\myDir\myBat.bat'"
I hope it helps someone someday.

Running batch file, call another batch, ctrl+c out of that, then return to the first?

I have a batch script which mount's a .vhd file and creates a junction point, the it call's another batch script which runs some things on the mounted vhd/junction point. When that script finishes, it then comes back to the 1st batch script and deletes junction point, unmounts .vhd then exits.
The problem is the second script needs to exit properly in order to run the rest of the first script, when most of the users will just ctrl+c and close the second script.
Is it possible to prevent this from happening, either by not allowing the ctrl+c cancel or somehow restart the first batch file after the call has happened?
try this to start the 2nd batch file:
start /b /w "" 2nd-batch.bat
Thanks to Endoro's answer, even though it didn't work for me, it made me remember start, i always forget start and just use call. anyway the solution i found was was to do this:
2>nul (
echo N|start /wait "" cmd /c C:\2nd-batch.BAT
)
it creates a new window for the 2nd-batch.bat which allows the dumbuser to even exit it via the X button and it still completes the rest of the 1st-batch.bat

Run batch files sequentially

I want to ask you all how to run batch files sequentially in Windows.
I have tried :
start /w batchfile_1.bat
start /w batchfile_2.bat
..
start /w batchfile_n.bat
but I have to close the previous .bat file process manually (e.g. by clicking) before continuing into the next one.
Is there any solution to do this automatically without me doing the manual closing previous .bat program every time?
Thanks a lot.
I would check the solutions to this question: Run Multiple batch files
Taken from the answer in the link.
Use call:
call bat1.cmd
call bat2.cmd
By default, when you just run a batch file from another one control will not pass back to the calling one. That's why you need to use call.
Basically, if you have a batch like this:
#echo off
echo Foo
batch2.cmd
echo Bar
then it will only output
Foo
If you write it like
#echo off
echo Foo
call batch2.cmd
echo Bar
however, it will output
Foo
Bar
because after batch2 terminates, program control is passed back to your original batch file.
If you are in love with using START, you could have your batch files end with the EXIT command. That will close the windows created by the start command.
#echo off
.
.
:: Inspired coding
.
.
exit
I'm not sure but based on your comments, the following seems to be happening when you run that sequence of START commands:
A START /W command is invoked and starts a batch file.
The batch file starts executing and runs a program.
The batch file finishes and its console window remains open, but the program continues running.
The START /W command that was used to run the batch file is still executing because the console window remains open.
You wait until the program terminates, then you close the console window, and then the next START /W command is invoked, and everything is repeated.
Now, if you place EXIT at the end of every batch file you want to run sequentially, that makes situation worse because it causes the console window to close after the batch script completes, and that in turn ends the corresponding START /W command and causes another one to execute, even though the program invoked by the batch script may still be running. And so the effect is that the batch scripts (or, rather, the programs executed by them) run simultaneously rather than sequentially.
I think, if this can be solved at all, you need to move the START /W command and put it in every batch file in front of (every) command that that batch file executes and doesn't wait for the termination of. That is, if your batchfile_1.bat runs a program.exe, change the command line to START /W program.exe, and similarly for other relevant commands in other batch files.

Resources