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

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:

Running Tasks List

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!



Solution 1:[1]

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

Solution 2:[2]

you can add "exit" to last line of your script

cd "C:\Users\user\Desktop"
echo. 2>test.txt
exit

Solution 3:[3]

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.

Solution 4:[4]

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 :)

Solution 5:[5]

Use following

exit /B

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

Solution 6:[6]

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.

Solution 7:[7]

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!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rob
Solution 2 mathmut
Solution 3 Mark
Solution 4 James
Solution 5 Mohsen
Solution 6 John Hon
Solution 7 David Ferenczy Rogožan