RunScheduledTask didn't execute itself the first time, why?

5

2

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? ;)

Francesco

Posted 2012-02-02T15:59:30.113

Reputation: 571

Notice that in v10 there is ScheduledTask[] which supports DateObject patterns and cron spects. So for this case the spec would be DateObject[{_,_,_,5}] Unfortunately it only works in the cloud. See http://reference.wolfram.com/language/ref/ScheduledTask.html

– Gustavo Delfino – 2015-10-06T13:19:40.640

2Shouldn'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 – 2012-02-02T16:24:47.000

1Yeah, 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 – 2012-02-02T16:55:04.753

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 – 2012-02-02T17:04:23.740

Answers

3

Can't you do something like

RunScheduledTask[Pippo[]; RunScheduledTask[Pippo[], 60*60*24], {0}, ABSTIME]

Heike

Posted 2012-02-02T15:59:30.113

Reputation: 34 748

1Yes, 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 – 2012-02-03T09:13:01.540

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 – 2012-02-03T19:25:57.643

1Aahahah 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 – 2012-02-06T09:28:49.743

2

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.

Matariki

Posted 2012-02-02T15:59:30.113

Reputation: 3 162

1If 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 – 2012-02-03T08:33:22.740

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 – 2012-02-03T09:08:41.637

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 – 2012-02-08T00:45:49.333