Created
February 28, 2026 08:14
-
-
Save me-suzy/1a4c678cf1c3f4e2e33b032bcabcb162 to your computer and use it in GitHub Desktop.
wait_until_430_then_run.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Pornește la logon (task "At log on"). Așteaptă până la 04:30, apoi închide Firefox și rulează .bat-ul. | |
| # Rulează în sesiunea utilizatorului, deci taskkill vede Firefox. | |
| import subprocess | |
| import time | |
| from datetime import datetime | |
| LOG_DIR = r"D:\TEST\Logs" | |
| BAT_PATH = r"D:\TEST\Run_PDF_Downloader.bat" | |
| TARGET_HOUR = 4 | |
| TARGET_MINUTE = 30 | |
| def log(msg): | |
| try: | |
| import os | |
| os.makedirs(LOG_DIR, exist_ok=True) | |
| path = os.path.join(LOG_DIR, "wait_until_430.log") | |
| with open(path, "a", encoding="utf-8") as f: | |
| f.write(f"{datetime.now():%Y-%m-%d %H:%M:%S} {msg}\n") | |
| except Exception: | |
| pass | |
| def seconds_until_430(): | |
| now = datetime.now() | |
| target = now.replace(hour=TARGET_HOUR, minute=TARGET_MINUTE, second=0, microsecond=0) | |
| if now >= target: | |
| from datetime import timedelta | |
| target += timedelta(days=1) | |
| return (target - now).total_seconds() | |
| def kill_firefox_and_geckodriver(): | |
| CREATE_NO_WINDOW = getattr(subprocess, 'CREATE_NO_WINDOW', 0x08000000) | |
| for name in ["firefox", "geckodriver"]: | |
| try: | |
| subprocess.run( | |
| ["taskkill", "/F", "/IM", f"{name}.exe"], | |
| capture_output=True, | |
| creationflags=CREATE_NO_WINDOW, | |
| timeout=15, | |
| ) | |
| except Exception as e: | |
| log(f"taskkill {name}: {e}") | |
| time.sleep(3) | |
| def main(): | |
| log("Wait_until_430 pornit (la logon). Aștept până la 04:30.") | |
| secs = seconds_until_430() | |
| log(f"Secunde până la 04:30: {secs:.0f}") | |
| if secs > 0: | |
| time.sleep(secs) | |
| log("E ora 04:30 - închid Firefox/geckodriver și pornesc .bat.") | |
| kill_firefox_and_geckodriver() | |
| log("Pornesc Run_PDF_Downloader.bat") | |
| try: | |
| subprocess.run( | |
| ["cmd", "/c", BAT_PATH], | |
| cwd=r"D:\TEST", | |
| timeout=3600 * 24, | |
| ) | |
| except Exception as e: | |
| log(f"Eroare la rularea .bat: {e}") | |
| log("Gata.") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment