Home > Enterprise >  How to run a task at a certain time every day?
How to run a task at a certain time every day?

Time:05-26

I need to run a task at a certain time of day in python, assuming the code is running. Can someone help me? I tried using the schedule api but it gives me an error. Are there any alternatives?

CodePudding user response:

If it's a simple script you could use a crontab to run your script at an interval. Here is a useful link to help you figure out what interval you want to run your script at: https://crontab.guru/

Can you please provide more context about the reason you need to schedule on an interval?

Celery and supervisor is another idea but it might be overkill for what you are trying to achieve.

CodePudding user response:

Your question is lacking detail so I can't give you a specific answer, here are a couple solutions you could pursue:

  1. Create a Crontab which triggers to run your python code (Since you said "assuming the code is running"; this might not be the right solution.)
  2. Put your code you want to run on a schedule in its own thread using the threading module, where it's on an infinite loop which checks if it's time to run the code yet.

CodePudding user response:

I'm wondering if you could share a little more information about your trouble with the schedule library. Maybe we could solve that error so you can use it!

Anyway, is schtasks (Windows), cron (Linux, Mac deprecated), or launchd (Mac) if you don't need to be cross platform? This is a more band-aid solution if you can't figure out schedule.

  • Related