How to terminate a batch file - cmd

While this question has been asked before, I have not found the solution to my particular problem. I am trying to launch the windows calculator from a bat file. It launches the program but the console window remains open. Typing exit or exit /b, does not help. This is what the file looks like:
C:\Windows\System32\calc.exe
exit
I tried typing exit before the launch command. It works and the program is not launched. It is as though the very act of launching the program renders all succeeding instructions null and void.

Related

Python.exe stays open after running batch file in task scheduler

I'm trying to schedule in task scheduler:
Run a batch file
Activate a conda environment
Run a python program
Exit command prompt
Everything works fine, except the python.exe window will remain open while the command prompt closes.
My batch file: (the sleep is for the python code to run. It takes a few seconds)
call activate python2
start C:\Users\Chris\Anaconda3\envs\python2\python.exe testtest.py
sleep 30
exit
My python script:
driver = webdriver.Chrome(executable_path="C:\path\to\chromedriver")
driver.get('http://website.com')
# Find email and pw fields and then fill them in
email = driver.find_element_by_id("user_email")
email.send_keys('myemail#email.com')
pw = driver.find_element_by_id("user_password")
pw.send_keys('password')
# Click on sign-in button
driver.find_element_by_class_name("button").click()
time.sleep(5)
# Click on save button to update
driver.find_element_by_class_name("button").click()
# Close driver
driver.close()
Last thing, the program/script is the batch file, no arguments, and the start in is in the directory that the batch file is in.
Any help would be appreciated!
put you python codes in a main() function.
and give:
if __name__ == '__main__':
main()
at the end.
Just tested works for me.
#pk2019 's answer really helped me.
One improvement is to use
drv = webdriver.Chrome()
# Do your things.
...
drv.close()
drv.quit()
No need to do the dirty work of killing task.
I'm not a python expert, but I think you just need to call sys.exit() or quit().
Instead of using sleep and waiting too long or possibly not long enough, call start with the wait option:
call activate python2
start /WAIT C:\Users\Chris\Anaconda3\envs\python2\python.exe testtest.py
exit
If you don't need the batch file to do anything else, you can just start the python script and exit.
I had a similar problem with a Python selenium web scraper running geckodriver.exe Firefox web driver on Windows (executed via Task Scheduler using a .bat file). The problem is that my geckodriver.exe process is still running after the Python script is done ... and I think that running process is preventing the Windows command prompt from closing.
In order to test this hunch, I inserted the tasklist command into the .bat file, both before and after the Python script. It prints out a list of running Windows tasks to the console ... I noticed the geckodriver.exe file was still running the Python script was finished.
The way to kill a Windows process (using taskkill) is described in these two different Stackoverflow responses:
Safely killing a python script on Windows
Batch script to close all open Command Prompt windows
How to close Command Prompt window after Batch file execution in python?
Here is the Windows documentation for taskkill:
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/taskkill
In my case, I added this as the 2nd to last line: taskkill /im geckodriver.exe /f, and the last line was exit. That worked.

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?

Close the command prompt window programmatically

I have a set of commands in batch file which is used to run the JAR file.It is working perfectly.
After running the program, the command prompt window doesn't get closed. If I manually close the window, then the application closes as well.
So I want to close the command window without affecting the application.
thanks in advance.
Either use exit or make a new method to go to and have nothing in it. The exit way is better though. :)
My psychic debugging powers tell me you're launching the JAR file using a java command instead of a javaw command.
Use javaw instead and the command window will close when the batch file exits.

how to make my console in python not to close?

I'm making a application in python from Windows. When I run it in the console, it stops, shows an error, and closes. I can't see the error becase its too fast, and I can't read it. I'm editing the code with IDLE (the program that came with python when I instaled it), and when I run it with the python shell, there are no errors. I would run it from IDLE, but when I use the console, it has more features.
I don't know why this is happening. I need your help.
Run the program from an already-open terminal. Open a command prompt and type:
python myscript.py
For that to work you need the python executable in your path. Just check on how to edit environment variables on windows, and add C:\PYTHON26 (or whatever directory you installed python to).When the program ends, it'll drop you back to the CMD windows prompt instead of closing the window.Add code to wait at the end of your script. Adding ...
raw_input()
... at the end of the script makes it wait for the ENTER key. That method is annoying because you have to modify the script, and have to remember removing it when you're done.
Run your program from a Windows command prompt. That will not automatically close when the program finishes.
If you run your program by double-clicking on the .py file icon, then Windows will close the window when your program finishes (whether it was successful or not).
Create a text file in the program directory i.e. wherever your script is located. Change the extension to .bat for example text.bat. Then edit the text file and write:
python main.exe
pause
Now you can run the program without typing into the command console by double clicking the bat file, and the console window will not close.

How to return an error code from a process that runs in another cmd window (windows)

I'm running a script from the cmd prompt. This script opens another cmd prompt and runs another batch file there. I want to wait for the error code and then send it back to the original cmd window. Is there a nice way to do this without writing the error code to a file?
Thanks,
Li
If I inderstand you correctly, you want this solution. It solves the problem of returning error level to the calling script from the script that was run in a separate cmd session.

Resources