0

I can't figure out why my Python script works perfectly in the console when I execute it like this (runs for 1-2 seconds):

Execution result

But if I run it in the task scheduler (either manually or at the scheduled time), it runs forever and eventually times out after the 2 hours time limit:

Task Scheduler history

Here's the script in case it's needed, I even added exit(0) at the end in case it was hanging there (Downloads all zip files over FTP, renames them according to their modification date, then deletes them on the server):

import ftplib
import os
from pathlib import Path
from datetime import datetime, timezone

# FTP server details
FTP_SERVER = "domain.com"
FTP_USERNAME = "username"
FTP_PASSWORD = "password"

# Directory for downloaded files
LOCAL_DIR = Path(r"G:\Neverland Backups")

# Connect to the FTP server
ftp = ftplib.FTP(FTP_SERVER, FTP_USERNAME, FTP_PASSWORD)

# List all files in the current directory on the server
files = ftp.nlst()

for file in files:
    # Check if the file is a .zip file
    if file.endswith('.zip'):
        # Download the file
        local_file = LOCAL_DIR / file
        with open(local_file, 'wb') as fp:
            ftp.retrbinary('RETR ' + file, fp.write)

        # Get the file modification time
        modification_time = ftp.sendcmd('MDTM ' + file)
        modification_time = datetime.strptime(modification_time[4:], "%Y%m%d%H%M%S")

        # Rename the file
        renamed_filepath = LOCAL_DIR / f"Neverland_{modification_time.strftime('%Y-%m-%d_%H.%M.%S')}.zip"
        (LOCAL_DIR / file).rename(renamed_filepath)

        # Convert the modification time to local timezone
        modification_time = modification_time.replace(tzinfo=timezone.utc).astimezone(tz=None)

        # Set the file's modification time
        timestamp = modification_time.timestamp()
        os.utime(renamed_filepath, (timestamp, timestamp))

        # Delete the original file on the server
        ftp.delete(file)

# Close the connection
ftp.quit()
exit(0)

As a sidenote, I also tried the Windows "Run" command with C:\Users\Administrator\AppData\Local\Programs\Python\Python312\python.exe "C:\scripts\download-neverland-backups.py" and the script window opens, executes then closes.

The task scheduler "actions" settings Task Scheduler "actions" settings

2
  • You might try using pythonw.exe instead. This will have been included in your installation alongside python.exe. (This is how I setup my own python scripts in Task Scheduler. I believe the issue is that scheduled tasks aren't provided with parts of the environment that you get when running in the console. pythonw.exe takes care of that but doesn't give you a UI.) Commented Apr 18, 2024 at 17:20
  • That was a good suggestion, didn't work unfortunately Commented Apr 19, 2024 at 4:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.