0

I am new to python and im trying to make a script. I want the script to keep looking at the time and for every 10 minutes i want to execute another command. So far i've got:

import time

min = time.strftime("%M")

How should i approach the rest?

it should run as some kind of service... it should be someting like:

if min == 00, 10, 20, 30, 40 or 50
    do this
dhojgaard
  • 113
  • 10
  • do you want to run a task every 10 minutes, or does it have to run at exactly 10, 20, 30, etc. minutes past the hour? – MattDMo Jan 09 '14 at 19:44
  • 14
    why not run it as a cron job? cron'll take care of the scheduling and then your script can simply focus on the actual job. Cron can schedule in 10 minute intervals quite easily. – Marc B Jan 09 '14 at 19:44
  • 1
    Check this out: http://stackoverflow.com/q/8600161/1510289 – Velimir Mlaker Jan 09 '14 at 19:46
  • I want it to run exactly at 00, 10, 20, 30, 40, 50, and i want to try to avid con jobs... – dhojgaard Jan 09 '14 at 19:48
  • Should it run once? Run as many times as possible in those minutes? Look into: `while` loops and what `min % 10` equals. – jonrsharpe Jan 09 '14 at 19:49
  • it should run as some kind of service... it should be someting like: if min == 00, 10, 20, 30, 40 or 50 do this or that – dhojgaard Jan 09 '14 at 19:50
  • What operating system? How one implements "some kind of service" varies greatly between operating systems. – Robᵩ Jan 09 '14 at 19:51
  • 2
    Tell us more about why you want to avoid cron. It is one (the easier one) of two ways that "some kind of service" is implemented in debian. – Robᵩ Jan 09 '14 at 19:52
  • rob, i know how to make it a service.. I just do not know how to make the script... – dhojgaard Jan 09 '14 at 19:53
  • rob, well basically because it theoretically should not be every time minutes, but every time the clock says 00, 10, 20 etc, and also because i have a feeling that it shouldn't be very hard in python... – dhojgaard Jan 09 '14 at 19:54
  • Time for a punt at the new close reasons, I think. This qualifies as _'Unclear what you're asking'_. Please clarify your requirement, and edit your question to include more information. –  Jan 09 '14 at 19:55
  • 1
    Are you familiar with the syntax of the crontab file? `0,10,20,30,40,50 * * * * /home/dhojgaard/myscript.py` does precisely what you are asking for. – Robᵩ Jan 09 '14 at 19:56
  • ``*/10 * * * * /home/dhojgaard/myscript.py`` is more terse, same thing though. – Velimir Mlaker Jan 09 '14 at 19:57
  • Yeah, i know that rob, but im just a guy trying to learn python while solving this task.. I am a nerd and just because there is an easy way it does not always mean you should go that way.. I wouldn't learn anything new by that... – dhojgaard Jan 09 '14 at 19:58
  • Part of learning a new tool is learning when not to use it. As a high level interpreted language Python is just not a good choice for scheduling. Keeping a python instance up and running just to check the time makes very little sense when they're already a cron process doing exactly this within your OS. – Ian Burnette Jan 09 '14 at 22:26

2 Answers2

0

This will do what you want using Python. Although from reading the comments I doubt it's the most efficient means of doing so:

import time


while True:
    curr_min = time.strftime('%M')
    curr_sec = time.strftime('%S')
    if int(curr_min) % 10 == 0 and int(curr_sec) % 10 == 0:
        print('Make script go now')
        time.sleep(59)

It checks once a minute if the minutes are a multiple of 10 and executes if it is.

UPDATE: This should do what you asked but it's really starting to be more apparent that another method would be a more logical/efficient fix

kylieCatt
  • 10,672
  • 5
  • 43
  • 51
  • But how do you start it at exactly 00 seconds? – Velimir Mlaker Jan 09 '14 at 20:00
  • A stopwatch? The OP asked for something that ran if the minutes are 00, 10, etc and this will do that. If he wants it at exactly 00:00 he should have included that in his question. – kylieCatt Jan 09 '14 at 20:01
  • Fair enough. But eventually you're gonna miss a ten minute slot because the lines before ``sleep()`` use up wallclock time and your ``sleep(60)`` will sleep through the whole 10 minute spot. – Velimir Mlaker Jan 09 '14 at 20:06
  • If i want it to be exactly when the time turns to 00, 10, 20 etc how should i approach it? – dhojgaard Jan 09 '14 at 20:07
  • To address @dhojgaard's issue, you switch to `int(curr_min) % 10 == 9` and then sleep at 1 second intervals until `int(curr_min) % 10 == 0`. On the other hand, @dhojgaard, if you need as "exact" as possible, then you should look into a real-time extension. – John1024 Jan 09 '14 at 20:13
0

I think this does the trick:

import time

while True:
    now = time.localtime()
    if now.tm_min % 10 == 0:
        # Do what you need to do
        time.sleep(559 - now.tm_sec) #sleeps until roughly the next 10 minute mark

That said, you should really use a cron job...

Ian Burnette
  • 1,020
  • 10
  • 16
  • If you start this before a 10 minute mark it should execute at :00 every time provided that whatever code you need to run doesn't take up much time (i.e. no more than a millisecond or two). If, for some reason, the loop starts a few seconds late it should automatically correct for that and hit the :00 mark next time around. It also won't spinning its wheels the whole time, it only get's hyper alert right around the 10 minute mark. Again though, use cron. You can even code the actions in python and just use cron to run "$python my_script.py" every 10 minutes. – Ian Burnette Jan 09 '14 at 20:23