简体   繁体   中英

Windows task scheduler terminates python script, opcode 2

I have a Python script that's designed to run "forever", which in this case is until the next scheduled reboot every night at 25 minutes past midnight.

What it does, is to check the time, then wait for the next full hour. Each hour it will parse some data from a webpage, store it in a database, and send an email to an external partner.

It starts on system boot using Windows Task Scheduler.

It's been running flawless for a couple of months, until midnight last sunday. The computer rebooted as usual at 0:25. After that, the log shows that the script is started, then terminates pretty much immediately with return opcode 2. Absolutely nothing I can think of changed to cause this, except perhaps a Windows update of some sort.

Any ideas what could cause this behaviour?

This is a pseudocode, all distractions removed, of what the script will see on the first run:

scriptRunning = True
timeoutDelay = -1

def getTimeoutDelay(lastFetchTime):
    # calculate and return timeout delay in seconds
    delay = (60 - int(lastFetchTime.strftime("%M"))) * 60 - int(lastFetchTime.strftime("%S"))
    return delay
# end def


while scriptRunning:
    if timeoutDelay > -1:
        #Do the magic
        pass

    lastFetchTime = datetime.now()
    timeoutDelay = getTimeoutDelay(lastFetchTime)

#endwhile scriptRunning

The script appears to run as designed if I start it from the command line manually.

You can put the file in your Startup folder, located at

%userprofile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs

This is where it usually is. You can also find it in Windows > All Programs > Startup

Alternatively, you can use a cmd command to create a task to do what you want specifically, for example you can run this in a cmd prompt in the folder your script is in

schtasks /create /TN "<name of task>" /SC ONLOGON /TR "%cd%\script.py" /Delay 0001:00

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