简体   繁体   中英

a problem with running a python script automatically on boot or with crontab

I have a script that runs my Telegram bot on PythonAnywhere. My PythonAnywhere account is free but limited and it restarts after 24 hours or less, I'm not sure of the exact time, and my bot turns off too. So, I made a script to run the bot automatically after 24 hours from my PC. When I run the script normally, it works well. But when I put it in the startup list to run automatically, it didn't. And after I put it in the crontab list, it still didn't run.

It's my script:

#!/usr/bin/env python

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from datetime import datetime as dt
from credentials import username, password
import time
import notify2

# Open a web browser and navigate to the website
driver = webdriver.Firefox()
wait = WebDriverWait(driver, 20)
driver.get("https://www.pythonanywhere.com/login/?next=/")

# Locate the login form and enter user & pass, then click on login
wait.until(EC.presence_of_element_located((By.ID, "id_next")))
driver.find_element(By.ID, "id_auth-username").send_keys(username)
driver.find_element(By.ID, "id_auth-password").send_keys(password)
driver.find_element(By.ID, "id_next").submit()

# Locate the bot link and click on it and wait to load the console
wait.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "NoNameCh")))
driver.find_element(By.PARTIAL_LINK_TEXT, "NoNameCh").click()

# Locate the run button and click on it then quit the browser and wait
driver.find_element(By.CSS_SELECTOR, "button.btn-info.run_button").click()
time.sleep(20)
driver.quit()

# Show notification
notify2.init("Automation")
notification = notify2.Notification("Bot Started!")
notification.set_timeout(10)
notification.show()

# Write a result in the file
path = "~/Dropbox/Projects/python/mypy/automation/result.txt"
with open(path, "a") as f:
    f.write(str(dt.today()) + "\n")

I made another script and put it in the crontab list. It's a script that, when run, returns a word in a specific file that I know it starts. After each 24 hours, the second script will return the result, but my primary script won't do anything.

It's the second script:

#!/usr/bin/env python

path = "~/Dropbox/Projects/python/mypy/automation/result.txt"
with open(path, "a") as f:
    f.write("test file...\n")

And it's the result file after two days:

2023-01-08 18:04:07.526809
test file...
test file...

The first line has appended when I ran the script manually. Normally, I should have gotten two results (the time when the script runs, and 'test file...').

What's the problem?

I found the answer. A script that uses GUI elements won't work in crontab because scripts in crontab run in the terminal. I haven't found a way to run my script automatically yet. I'm still searching...

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