5

I'm trying to schedule in task scheduler:

  1. Run a batch file
  2. Activate a conda environment
  3. Run a python program
  4. 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!

Chris
  • 61
  • 1
  • 4
  • Anyone else figure this out? Because none of the "solutions" worked for me and the script just hangs after completion if ran from Task Scheduler. I'll keep on troubleshooting and maybe make some kind of batch script to handle it, but I'm stuck right now. Thanks! – jacktrader Sep 21 '21 at 21:30

4 Answers4

3

put you python codes in a main() function.

and give:

if __name__ == '__main__':
    main()

at the end. Just tested works for me.

Derrick
  • 3,669
  • 5
  • 35
  • 50
JerryLong
  • 79
  • 1
  • 9
3

@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.

  • Thanks this one worked for me with task scheduler because even though I selected the option to close the instance before opening a new one, the python consoles remained open and I ended up with a bunch consoles. – Wak Sep 13 '21 at 19:53
0

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.

DSway
  • 752
  • 14
  • 33
  • The /WAIT works perfectly, but the sys.exit() and quit() doesn't work when written in the Python script. – Chris Sep 30 '17 at 17:20
  • I think maybe what needs to be exited is the console window created by the start command and not the python script. Maybe you'd be better off just using another call statement instead of start. Do you need to have the python script run in a separate window? – DSway Sep 30 '17 at 22:45
  • I actually do not want the python script to run in a separate window. If it ran in the same one, that would be great. I tried to just call the python statement, but now the command prompt does not close. It displays the exit command, but doesn't actually close. – Chris Oct 02 '17 at 11:31
  • Is python still running (task manager)? Does the console window close if you manually type close? – DSway Oct 02 '17 at 12:47
  • Python is still running, and the console window will not let me type anything inside. I can exit with the x button or pressing ctrl+c. – Chris Oct 03 '17 at 14:28
0

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:

  1. Safely killing a python script on Windows
  2. Batch script to close all open Command Prompt windows
  3. 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.