Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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('[email protected]')
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!

like image 887
Chris Avatar asked Sep 29 '17 21:09

Chris


People also ask

How to schedule a python script in Windows Task Scheduler?

Using Windows Task Scheduler. Step 1: Create a Batch File. Open Notepad and follow this generic structure : “Path where your Python exe is stored\python.exe” “Path where your Python script is stored\script name.py” Finally, save the Notepad with your file name and the “.bat” extension anywhere on your machine, Eg – ‘automation.bat’.

How to schedule a batch file in Python?

At any given point in time, in addition to prescribed times, the user can simply double-click on the batch file and get the output from Python. Finally, you my want to visit the task scheduler documentations to find out more about scheduling tasks.

How to run Python scripts on Windows 10?

For this step, I’m going to use Windows 10 to execute the Python Script via the Windows Scheduler. Similar principles would apply when using previous versions of Windows. First, open the Control Panel and then click on the Administrative Tools: Next, double-click on the Task Scheduler, and then choose the option to ‘Create Basic Task…’

How do I run pytest from a batch file?

Use Task Scheduler run a batch file. The batch file runs pytest and captures our pytest function results to sys.


2 Answers

put you python codes in a main() function.

and give:

if __name__ == '__main__':
    main()

at the end. Just tested works for me.

like image 95
JerryLong Avatar answered Oct 05 '22 23:10

JerryLong


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

like image 25
Xavier Xing Avatar answered Oct 05 '22 23:10

Xavier Xing