Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a problem with RunScheduledTask (new in Mathematica 8).

A task that I usually need to do is to set a cron to start at a specific time and repeat itself at the same time every day (or the same minute every hour, and so on...)!

Let's make an example: suppose I have to run Pippo[] at 5:00 AM every day starting from tomorrow. These are the "logic" steps in my mind to do so:

  • calculate the absolute time of tomorrow at 5:00 and save it to ABSTIME
  • execute RunScheduledTask[Pippo[], 60*60*24, ABSTIME]

Unfortunately I have discovered that if I make it this way IT WON'T EXECUTE tomorrow at 5:00, but it will start the day after. I tried to set as ABSTIME the absolute time of today at 5:00 but it doesn't work either ('cause the ABSTIME is in the past). The only work-around I found is:

  • calculate the absolute time of tomorrow at 5:00 and save it to ABSTIME
  • calculate amount of seconds from now to tomorrow at 5:00 and save it as SECSREMAINING
  • execute RunScheduledTask[Pippo[], {SECSREMAINING}] for the execution of tomorrow
  • execute RunScheduledTask[Pippo[], 60*60*24, ABSTIME] for all other executions

I think this a very "an-elegant" way, do you have any other solution? Or should we mass-warn Wolfram about this and make it change? ;)

share|improve this question
2  
Shouldn't a simpler workaround be to calculate ABSTIME of 5:00 today and use that in the call to RunScheduledTask? I haven't tested it, but from the described behaviour it seems to me it would start for the first time tomorrow at 5:00, as intended. –  celtschk Feb 2 '12 at 16:24
    
Yeah, I thought "today" and wrote "yesterday", sorry, I'll correct as soon as I finish this comment. The problem is that it won't accept an ABSTIME in the past :( ...so if I calculate 5:00 of today it does nothing :( –  Francesco Feb 2 '12 at 16:55
    
I'll make an example, suppose you run this command: RunScheduledTask[Pippo[], 10, AbsoluteTime[]-5], following your reply it should execute Pippo[] after 5 seconds, but it executes Pippo[] after 10 seconds, exactly the same results as launching the command without a first execution time specification. –  Francesco Feb 2 '12 at 17:04
add comment

2 Answers

up vote 2 down vote accepted

Can't you do something like

RunScheduledTask[Pippo[]; RunScheduledTask[Pippo[], 60*60*24], {0}, ABSTIME]
share|improve this answer
    
Yes, I can do this, I can even do: RunScheduledTask[Pippo[]; RunScheduledTask[Pippo[], 60*60*24], {SECSREMAINING}] but this is not "elegant" and this create two distinct events in ScheduledTasks[]'s list, this is ugly :) ...anyway, I set your answer as my accepted one 'cause I don't think there's a better solution! –  Francesco Feb 3 '12 at 9:13
    
It's not making things more elegant, but if you only want one event in the ScheduledTasks list at the time you could do something like task=RunScheduledTask[Pippo[]; RunScheduledTask[Pippo[]]; RemoveScheduledTask[task], 60*60*24], {SECSREMAINING}]. –  Heike Feb 3 '12 at 19:25
    
Aahahah yes, I know that this works, but I think that the guys at Wolfram just have to change the behavior of this function :) Thanks a lot anyway ;) –  Francesco Feb 6 '12 at 9:28
add comment

I have tried this on both a mac and a pc using MMa 8.0.4 and it works as advertised. If the time is in the past the task will execute instantly. I have tried the following and got the expected result:

RunScheduledTask[MessageDialog["text"], {1}, AbsoluteTime[] + 5]
RunScheduledTask[MessageDialog["text"], 5, AbsoluteTime[] + 5]

If I execute the code below with a time in the past I get the dialog straight away.

RunScheduledTask[MessageDialog["text"], {1}, AbsoluteTime[] - 5]

Are you running 8.0.1 or 8.0.4?

Edit

I think I do understand now what you are after and have the following code snippet

repetitionTime = 10;
scheduledStartTime = 5;

printMessage[] := Print["Task executes. Time: ", DateString[]]

RunScheduledTask[
 Block[{}, ResetScheduledTask[$ScheduledTask, {repetitionTime}]; 
  printMessage[]; 
  StartScheduledTask[$ScheduledTask]], {scheduledStartTime}]; \
Print["Start time: ", DateString[]]

This task will start after scheduledStartTime seconds and will execute every repetitionTime seconds. It is not adding the repetition time to the start time. In this case the first printMessage is executed after 5 seconds and is then repeated every 10 seconds.

Hope this helps.

share|improve this answer
1  
If I run RunScheduledTask[MessageDialog["text"], {6}, AbsoluteTime[] - 5] on my on Mma 8.0.4 for OSX, I get the same result as the OP, i.e. it takes 6 second for the dialog to appear instead of 6-5==1. –  Heike Feb 3 '12 at 8:33
    
I have a PC and I'm running version 8.0.4. Anyway if I execute your last piece of code I get the dialog after 1 second and non instantly. Can you try the Heike's example and tell us if it runs after 1 second (like it should be) or after 6 seconds? –  Francesco Feb 3 '12 at 9:08
    
Sorry for the delay to come back to you. You are right the time is not 1 second but 6. I should have used a better stop watch or longer example times. However, I think I have a program snippet that is doing what you are after. I'll provide another answer shortly. –  Matariki Feb 8 '12 at 0:45
add comment

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.