Mohamed Abbase
Mohamed Abbase

Reputation: 159

python schedule to run another file.py every certain time

I have a python script in file called tasks.py containing multiple tasks, processes and functions (and it works perfect to me).

i want to run this file (tasks.py) at certain time ( 1:00 am & 4:45 am & 11:35 am & 6:25 pm & 9:10 pm) every day starting from date say as example 6 aug 2018.

so i created a new file and called it run.py

i used the following code to import tasks.py file

#!/usr/bin/python
import tasks

but i want to schedule this import in the certain times as mentioned above starting from the required date

i tried while function, schedule module, cron module and import os but i failed with all

any body can help please ????

Upvotes: 0

Views: 1147

Answers (1)

Prysiazhnui max
Prysiazhnui max

Reputation: 21

The simple solution can be something like that

import datetime
from time import sleep

rest_seconds = 3600  # sleep for one hour, so the task won't be called few times per hour
while True:
    if datetime.datetime.now().hour == 12:
        do_task()
    sleep(rest_seconds)

If you need a complex one, I use Airflow framework for building scheduled tasks/pipelines

Upvotes: 2

Related Questions