简体   繁体   中英

How to tell a Python script to close after running

I'm a complete beginner in python... I wrote a simple script that pulls some data from a website and saves it to a text file, but after running the script it just sits there and doesn't "terminate". Since I'll be running it as a windows task I need it close on its own after running. Is there a way to tell python to close the terminal after the script finishes? I've looked everywhere and it seems that most people are trying to do the opposite, keep it open after the code runs. Not sure if it matters but I'm just running the .py file, not in VS or PyCharm. Thanks in advance.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from datetime import date




driver = webdriver.Chrome()
driver.get("MyURL")
time.sleep(11)
first = driver.find_element_by_xpath('/html/body/main/div[2]/div/div/div/div[2]/div[1]/div/div[2]/div[2]/div/ul/table/tbody/tr[3]/td[2]/div/span[1]')
second = driver.find_element_by_xpath('/html/body/main/div[2]/div/div/div/div[2]/div[1]/div/div[2]/div[2]/div/ul/table/tbody/tr[2]/td[2]/div/span[1]')
third = driver.find_element_by_xpath('/html/body/main/div[2]/div/div/div/div[2]/div[1]/div/div[2]/div[2]/div/ul/table/tbody/tr[1]/td[2]/div/span[1]')

first = first.text
second = second.text
third = third.text

print(first, second, third)

today = date.today()
today1 = today.strftime('%b %d %Y')

list = (today1, first, second, third)
print(list)

file = open('test.txt', 'a')
file.write(today1)
file.write(' ')
file.write(first)
file.write(' ')
file.write(second)
file.write(' ')
file.write(third)
file.write('\n')
file.close()


driver.close()

For 1. you need to send your running process to the background and remove the associated job from current shell.

Press Ctrl + Z and type bg to send the installation process to the backgroud then type disown .

You can now close the terminal, the process will still be alive. You can open another terminal and check its process id with ps -aef

In my case the process id is 14426 . Unfortunately there's no easy way to reattach it to another terminal (See How to attach terminal to detached process?) unless you used something based on screen .

For 2. You can use the following command:

while kill -0 14426 >/dev/null 2>&1; do sleep 5 ; done ; echo "ok" while kill -0 14426 >/dev/null 2>&1; do sleep 5 ; done ; echo "ok" It will print ok when the process is over from an other terminal. You can of course change this echo command with something more complex.

Source: BASH: launch background process and check when it ends

Okay, I found something that worked. I noticed that the chromedriver process was still running even after the line driver.close() executed. I went ahead and killed the process and the script closed immediately. I guess driver.close() closes the chrome window but didn't kill chromedriver so the script was hanging waiting for the process to quit. I replaced driver.close() with driver.quit() and now the script closes after running. Thanks everyone for the help!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM