Python script never ends in task scheduler
Asked Answered
T

5

5

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 :

  • program : cmd

  • arguments : /c C:\python27\python.exe C:\path\of\script.py

  • execute in : C:\path\of\

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.

Toolmaker answered 22/2, 2016 at 13:7 Comment(3)
I wonder if it would terminate if you explicitly close the file afterwards.Dusa
exit status 0 represents successful completion of any task. try adding sys.exit(0) in the end, also close the file pointer before exit.Controller
Thanks for your suggestions, I added f.close() and sys.exit(0) to my code, but it didn't modify the issue : task still stays as "Running" when I launch it from task schedulerToolmaker
B
3

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!

Bremsstrahlung answered 16/5, 2020 at 18:10 Comment(1)
I tried this in a "finally:" statement and it didn't work. I will leave it in anyways to help make sure the script closes up correctly. I did knot about this. Thanks!Window
S
3

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.

Sausage answered 11/1, 2023 at 15:19 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Adorn
I did observe the same situation. I think what the author meant was to press the Refresh button under the Actions panel in the Task Scheduler applet.Teirtza
S
1

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'

Smaragd answered 12/11, 2017 at 17:20 Comment(1)
I just closed Python manually and there are no background process running Python. I suspect you are correct.Window
C
1

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.

Clodhopper answered 23/11, 2021 at 7:6 Comment(0)
A
0

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".

Alrich answered 22/8, 2024 at 8:53 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.