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

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

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

-
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
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 reference.wolfram.com/language/ref/ScheduledTask.html – Gustavo Delfino Oct 6 '15 at 13:19

Can't you do something like

-
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

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:

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

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[]]

Block[{}, ResetScheduledTask[$ScheduledTask, {repetitionTime}]; printMessage[]; StartScheduledTask[$ScheduledTask]], {scheduledStartTime}]; \