Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm running a Python program on my Linux server, and depending on some external data it has to run again at xx minutes or hours from now.

So let's say it runs at 6 AM, and then it has to run again at 7 AM. Then, at 7AM, it checks some things and it has to run again at 15:45, and then the next day at 2.05AM, and then the next day at 4.05AM etc.

As you can see there is no predefined logic in the times it has to run, it has to be defined at the time it runs.

The only task schedule mechanism I know is crontab, bu I'm not sure how to add tasks to it without running crontab -e and besides that crontab seems more for recurring tasks and in my case I would add a crontab job and after running it once remove it again and add a new one.

The only thing I could come up with was to set the next rundate in a textfile, and let a crontab job check it every minute to see if it is time to run the program.

share|improve this question
1  
Did you consider: en.wikipedia.org/wiki/At_%28Unix%29 instead of a crontab? –  Bernhard Mar 25 '13 at 14:23
    
No, I didn't know of that. Looks promising! –  Michel Mar 25 '13 at 14:47
    
@Bernhard, got it working, can you post it as an answer? –  Michel Mar 26 '13 at 16:51
    
No problem, just added an answer. –  Bernhard Mar 26 '13 at 18:17

3 Answers 3

up vote 1 down vote accepted

crontab should be used for jobs that you want to have repeated regularly. An alternative is at. With this utility you can schedule jobs that you want to execute only once, but in the future.

From within the python-script, you should be able to add a command to the queue of at. The link page together with the man-page should give you enough information to get going.

As per @Michel's comment, this will be

newruntime = (datetime.datetime.now() + datetime.timedelta(minutes=5)).strftime("%H:%M %d.%m.%Y")
command = 'echo " python mainprog.py" | at ' + newruntime
os.system(command)
share|improve this answer
    
Works like a charm. –  Michel Mar 26 '13 at 19:52
1  
my code: newruntime = (datetime.datetime.now() + datetime.timedelta(minutes=5)).strftime("%H:%M %d.%m.%Y") command = 'echo " python mainprog.py" | at ' + newruntime os.system(command) –  Michel Mar 26 '13 at 19:53

Python-crontab module looks promissing. With easy addition and removal of tasks.

Also there is a thread@stackoverflow with a few more handy ideas and links.

share|improve this answer
    
Hm, this looks good. Will give it a try. –  Michel Mar 25 '13 at 14:48
    
Works fine, but for the matter of changing intervals i like at just a little better –  Michel Mar 26 '13 at 16:52

Does time.sleep() not work for you? What I mean is: Can you not just pass the number of seconds to wait to sleep() and then let the script resume execution (possibly by doing a conditional sleep at the end of a loop)? It would reduce the number of external dependencies required by some of the other suggestions.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.