WNG
WNG

Reputation: 3805

Python script never ends in task scheduler

I am trying to set up a python code to be executed automatically.

I started with a small code to be executed:

import datetime
with open("out.txt","a") as f:
    f.write(datetime.datetime.now().isoformat())

The task will start allright, and executes (The file is modified), but it never ends in the task scheduler.

this and this exist in SO, but have no real answer. The only workaround proposed in these threads is to force the end of task after a given time in Windows, but this requires to know how long the python script will take which will not be the case for my actual task.

How can the task scheduler know that a python script is finished ?

I run it the following way in the task scheduler :

I tried some variations around this, like executing python instead of cmd, but it didn't change anything. I had hoped the /c would force the task to close.

Upvotes: 5

Views: 5495

Answers (5)

pshaw
pshaw

Reputation: 1

I had a similar (same?) issue where Task Scheduler Status indicated that my python programs were still running after they had terminated.

In my case, it turned out to be a Windows issue. It seems to be a failure to update Status.

The simple fix was to exit Task Scheduler completely.

On reentering, Task Scheduler Status showed as "Ready".

Upvotes: 0

I ran into the same problem, the python file didn't stop in the task scheduler. I imported sys and wrote sys.exit(0) but I still got the same problem. Finally, I decided to press "Update" which solved my problem; the status of the task was "Ready", and not "Running". For information, I use windows 11.

Upvotes: 3

Ben
Ben

Reputation: 1291

There seems to be no correct fix for such issue with CMD as the intermediate launcher. There is a [End] command in Task Scheduler GUI, clicking it will always terminate the CMD/batch file leaving the spawned python.exe process to straw.

The real problem: there doesn't seem to be any way for cmd to pass on the terminate signal to python.exe.. and neither can taskengine reliably determine if python.exe is alive or not.

Upvotes: 1

cyeow
cyeow

Reputation: 31

as Gaurav Pundir mentioned, adding sys.exit(0) should end the script properly and thus the task. However, you do need to add the sys library with import sys in order to use sys.exit(0). Hope this helps!

Upvotes: 3

user97662
user97662

Reputation: 960

it looks like a bug to me. Try looking up for python console under Task Manager. if it is not there then the program has exited successfully.

I have the same issue with Windows 10, python script ran successfully, there is no python console under Task Manager, yet the scheduled task Status still says 'Running'

Upvotes: 1

Related Questions