Forums

Scheduled Tasks with planned restarts

Apologies if this has been asked before.

I have a few tasks that I like to keep running around the clock. It’s helpful to have them restart if they are killed, but sometimes I would like them restarted once a week, even if there is no problem.

Is this possible?

If you want something to start running and then keep running forever, then you'd need to use an always-on task rather than a scheduled task. However, if that's what you meant -- a good way to periodically restart them would then to use the API to tell the system to restart them at a particular time, via a scheduled task. While scheduled tasks can only be scheduled hourly or daily, you could get one to perform an action by scheduling it daily, and then checking the day of the week at the start of the script and exiting early if it's not (say) Sunday:

import sys
from datetime import datetime

# Check if the current day is Sunday (Sunday is represented by 6 in Python)
if datetime.today().weekday() != 6:
    sys.exit()

Thanks, Giles. Thats a great idea. I will update my code to poison the well periodically to force a restart. Cheers

No problem, glad to help!

I ended up replacing my “while true:” endless loop with one that counts down from 24 hours to 0 in 5 minute increments. When the loop ends, the script ends

Glad to hear that you made it work like you wanted it to.