0

I wish to schedule a python script for every 30 minutes. I am currently using the Timer to run the python script but it is not working precisely. I am using Linux. This code works fine on windows but is not working correctly on Linux. It should be triggered after half an hour but it gets triggered within a minute.

from datetime import datetime, timedelta
from threading import Timer
j=1
while True :
          x=datetime.today()    
          y = x + timedelta(minutes=10*j)
          print(y)
          delta_t=y-x
          secs=delta_t.seconds + 1


          def hello_world():
              print ("hello world")
              print("yes)")
    

          t = Timer(secs, hello_world)
          t.start()

          j=j+1`

Can anyone point out the mistake in above code or suggest an alternative to run the python script in linux after every 30 minutes?

Thank you

  • 1
    Please update the indentation of your code. Python is very sensitive to indentation, as are python programmers. – quamrana Jun 21 '20 at 09:51
  • Unless there is a specific reason why you want to have a long-running process which mostly sleeps but actively does something periodically, the usual method for doing tasks periodically on Linux is to use `crontab`. This is not internal to python, but rather a way to make the operating system invoke your python script (or any other command) periodically. – alani Jun 21 '20 at 10:10
  • An advantage in using a cron job is that you do not need to restart your process manually, for example after the system is rebooted. – alani Jun 21 '20 at 10:12
  • On the specifics of your code, you seem to be trying to create an infinite number of (simultaneous) threads. I can reproduce the fact that once you get to a certain number of threads, the timer for the first one finished unexpectedly quickly (in experimentation I found it happens when you reach 145 threads). I don't know if this could be considered a bug in the library, but I don't think it is reasonable for an application to create such a large number of threads. – alani Jun 21 '20 at 10:28
  • @alaniwi I tried using the crontab but I dont know why it gives an error - No such file or directory. I tried running the python command line, it works perfectly fine but crontab gives an error. – Nimesh Shahdadpuri Jun 21 '20 at 11:23
  • Have a look at this answer: https://stackoverflow.com/a/13151104/8840245 and it's follow up answer: https://stackoverflow.com/a/18906292/8840245, I think they both can solve your problem without using any external library. Cheers! – Mrinal Roy Jul 08 '20 at 06:51

1 Answers1

1

You could use Python’s schedule library.

import schedule

def hello_world():
    print ("hello world")
    print("yes)")

schedule.every(30).minutes.do(hello_world)

while True:  

    # Checks whether a scheduled task  

    # is pending to run or not 

    schedule.run_pending() 

Further information: https://www.geeksforgeeks.org/python-schedule-library/

Mrinal Roy
  • 969
  • 7
  • 12
Vate
  • 56
  • 11
  • Works well, though requires `pip install schedule`. For something using the stdlib, maybe just a simple `time.sleep` in a loop would work, although one would probably want to sleep until 30 minutes after the previous start time, to allow for the execution time of the task itself. – alani Jun 21 '20 at 10:43
  • @Vate I dont want to install any external library. – Nimesh Shahdadpuri Jun 21 '20 at 11:24