Restarting script

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
Fred
Newbie
Posts: 18
Joined: Sat Aug 05, 2017 4:44 pm

Restarting script

Post by Fred » Tue Jan 16, 2018 7:54 am

How can I stop and restart my script every n minutes/hours?

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Restarting script

Post by JRL » Tue Jan 16, 2018 4:15 pm

I have a couple of scripts that run perpetually but have to be restarted every twenty four hours. More or less this is what they do. The script uses a TIME OnEvent to trigger after an assigned interval (in Minutes). The OnEvent> runs a subroutine that reruns the script and closes the current iteration of the script. Also added a cycle count so you can see how many times the script has restarted.

Note: as presented the script restarts at the top of the minute (computer time seconds are at 0) rather than after the number of minutes. This might be an issue the first cycle depending on what you're script is doing as the first cycle could last only a few seconds. To avoid this issue, there is code at the beginning that holds the script until the current time is at the top of the minute. That code is remarked out and needs to be unremarked to function.

Note the remarks:

Hope this is a helpful sample.

Edit: Had to make a change because GetTime> did not put time in a 24 hour clock format and the TIME OnEvent> requires a 24 hour clock format.

Code: Select all

//Create a key_down OnEvent> for manually
//killing the script on Exc key press
OnEvent>key_down,VK27,0,Quit

//Conditioning the "command_line" variable to be used from
//the Macro Scheduler main menu or via opening the script
//from Windows Explorer or Windows command line.
StringReplace>command_line,script_file,,command_line
StringReplace>command_line,",,command_line
UpperCase>command_line,command_line
Separate>command_line,.EXE,ComPart
Let>command_line=%ComPart_1%.EXE

//Time interval in minutes to restart script
Let>vInterval=1

//Cycle counter
If>vCycle={"vCycle"}
  Let>vCycle=0
EndIf
Add>vCycle,1

//Get Current time and add the restart interval.
//This works on my computer with my regional settings.
//You my need to do things differently to acquire
//a two character hour and two character minute
//for the TIME OnEvent>.
Label>CheckTheTime
Hour>HH
Min>MM
Let>vNow=%HH%:%MM%
TimeAdd>vNow,m,vInterval,vStopTime
Separate>vStopTime,:,vSegment
Format>%.2d,vSegment_1,HH
Format>%.2d,vSegment_2,MM
If>HH<12
  Add>HH,12
EndIf

//Unremark the following block to cause first cycle to start
//at the top of the minute. 
/*
Sec>SS
If>{(%SS%=00)and(%vCycle%>0)}
Else
  Wait>0.01
  Goto>CheckTheTime
EndIf
*/

//Create a time OnEvent> for monitoring when to restart the script
OnEvent>TIME,%HH%%MM%,,QuitAndRestart

//Variable flag to prevent the TIME OnEvent> from running
//the QuitAndRestart subroutine multiple times. Value of the
//variable is changed to 1 (one) in the subroutine,  The subroutine
//Will restart the script only when the value of the variable
//is 0 (zero).
Let>QuitAndRestartFlag=0

//Dialog for time display in this sample
Dialog>Dialog1
object Dialog1: TForm
end
EndDialog>Dialog1

Show>Dialog1

//Loop for something to do in this sample
Label>Loop
  Wait>0.01
  GetTime>vNow
  SetDialogProperty>Dialog1,,Caption,Time is %vNow% Restart at %HH%:%MM% Cycle = %vCycle%
Goto>Loop

//At interval elapse, QuitAndRestart subroutine
//stops current script and restarts a new one.
SRT>QuitAndRestart
  If>QuitAndRestartFlag=0
    Let>QuitAndRestartFlag=1
    Let>RP_Wait=0
    RunProgram>%command_Line% %script_file% /vCycle=%vCycle%
  EndIf
  Exit>0
END>QuitAndRestart

//Pressing Esc key manually kills the script.
SRT>Quit
  Exit>0
END>Quit

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Restarting script

Post by JRL » Wed Jan 17, 2018 4:39 am

Another similar method but not using an OnEvent>. This method tests the elapsed time in a loop and runs the new iteration of the script just before exiting when the elapsed time matches the specified time interval.

Code: Select all

//Create a key_down OnEvent> for manually
//killing the script on Exc key press
OnEvent>key_down,VK27,0,Quit

//Conditioning the "command_line" variable to be used from
//the Macro Scheduler main menu or via opening the script
//from Windows Explorer or Windows command line.
StringReplace>command_line,script_file,,command_line
StringReplace>command_line,",,command_line
UpperCase>command_line,command_line
Separate>command_line,.EXE,ComPart
Let>command_line=%ComPart_1%.EXE

//Time interval in minutes to restart script
Let>vInterval=1
Let>vIntervalSeconds={%vInterval%*60}

//Cycle counter
If>vCycle={"vCycle"}
  Let>vCycle=0
EndIf
Add>vCycle,1

Timer>vStartTime

//Dialog for time display in this sample
Dialog>Dialog1
object Dialog1: TForm
end
EndDialog>Dialog1

Show>Dialog1

//Loop for something to do in this sample
Label>Loop
  Wait>0.01
  //Timer tests to see if the assigned time interval
  //has been reached.  When reached the QuitAndRestart
  //subroutine runs
  Timer>vEndTime
  Let>vTotalTime={round((%vEndTime%-%vStartTime%)/1000)}
  Let>vDisplayTime={%vIntervalSeconds%-%vTotalTime%}
  If>{(%vInterval%*60)<%vTotalTime%}
    GoSub>QuitAndRestart
  EndIf
  SetDialogProperty>Dialog1,,Caption,Restart in %vDisplayTime% Seconds  Cycle = %vCycle%
Goto>Loop

//At interval elapse, QuitAndRestart subroutine
//stops current script and restarts a new one.
SRT>QuitAndRestart
  Let>RP_Wait=0
  RunProgram>%command_Line% %script_file% /vCycle=%vCycle%
  Exit>0
END>QuitAndRestart

//Pressing Esc key manually kills the script.
SRT>Quit
  Exit>0
END>Quit

Fred
Newbie
Posts: 18
Joined: Sat Aug 05, 2017 4:44 pm

Re: Restarting script

Post by Fred » Wed Jan 31, 2018 10:47 pm

Thank you for your input. I just solved it this way:

Timer>ScriptRunTime
If>ScriptRunTime>3600000
Message>Restarting script
Wait>2
ExecuteFile>E:\Documents\Macro Scheduler 14\Decred18.scp,
Wait>1
Exit
Endif

I just didnt know i can exit the script by simple Exit command. Also, I didnt know how to restart it when the script is closed, so I solved it by simply starting the second copy for a second and closing the first one. Looks working.

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts