Jump to content
Sign in to follow this  
pantho

Continuously execute PHP script with random pauses

Recommended Posts

pantho

Hi

I looking for AutoIt code to continuously execute PHP script with random X sec delay between starts (where X is in interval FROM-TO)

Steps:

Step 1. wait random X seconds (X in FROM-TO interval)

Step 2. execute PHP interpreter+script: "c:\php\php.exe script.php"

Step 3. if scripts exit code==0 back to step 1, else warning and stop

+ button to terminate the AutoIt script

Thanks for help.

Share this post


Link to post
Share on other sites
MrMitchell

Hi and welcome to the forums.

You should be able to find everything you need in the Help File. We'll always recommend you get started with a basic AutoIt tutorial then once you've gone through some of the examples, check out the following commands specifically:

While...Wend loop
ShellExecute()
HotKeySet()

and the section about creating custom functions.

Post what you come up with along with questions if you have trouble.

Edit: Go if you need help with Help File and just getting started...

Edited by MrMitchell

Share this post


Link to post
Share on other sites
hannes08

I would suggest to use Run() or RunWait() to run the php scripts, as you'ss receieve the return code.


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites
pantho

Thanks for help, what do you think about this basic solution (any advice?):

HotKeySet("{ESC}", "ExitProgram")

Local $delay
Local $ret
Local $status
Local $next, $nday, $nhour, $nmin, $nsec

While 1
    $ret = RunWait("s:\php\php.exe -f script.php", "s:\php\")
    $status = "Last run: " & @HOUR & ":" & @MIN & ":" & @SEC & "     Return: " & $ret
    If ($ret=0) Then   ; if no error in scripting, adding next run info to status
        $delay = Random(3*1000, 6*1000, 1)   ; delay to next run
            $next  = @HOUR*60*60 + @MIN*60 + @SEC + Round($delay/1000)   ; calculating next run in HH:MM:SS format
            $nday  = Floor($next/86400)
            $nhour = Floor($next/3600) - ($nday*24)
            $nmin  = Floor($next/60) - ($nday*1440) - ($nhour*60)
            $nsec  = Mod($next, 60)
            ;$nhour = Floor($next/3600);
            ;$nmin = Floor(Mod(($next/60),60));
            ;$nsec = Mod($next,60);
        $status = $status & "    Next run: " & StringFormat("%02d",$nhour) & ":" & StringFormat("%02d",$nmin) & ":" & StringFormat("%02d",$nsec)
    EndIf
    ToolTip($status, 0, 0)   ; displaying status info
    If ($ret<>0) Then   ; if error in scripting, end program
        MsgBox(0, "Warning", "Return code: " & $ret)
        Exit
    EndIf
    Sleep($delay)   ; waiting for next run
WEnd

Func ExitProgram()   ; to terminating the program manually
    Exit
EndFunc

Share this post


Link to post
Share on other sites
hannes08

Hello pantho,

you can use _DateAdd() to calculate the new execution time, without having to use the calculations and StringFormat()s you're using, though it's a good lesson :)

Having a Tooltip pop up every 3 - 6 seconds may be annoying ;)


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites
pantho

Thanks for the tip ;-)

ToolTip is always visible in the corner, so there is no flickering

Here is the simplified version, any suggestion?

#include <Date.au3>

Local $delay, $ret, $status

While 1
    $ret = RunWait("s:\php\php.exe -f script.php", "s:\php\")
    $status = "Last run: " & StringRight(_NowCalc(), 8) & "  Return: " & $ret
    If ($ret=0) Then   ; if no error in scripting, adding next run info to status
        $delay = Random(3*1000, 6*1000, 1)
        $status = $status & "    Next run: " & StringRight(_DateAdd('s', Round($delay/1000), _NowCalc()), 8)
    EndIf
    ToolTip($status, 0, 0)
    If ($ret<>0) Then   ; if error in scripting, end program
        MsgBox(0, "Warning", "Return code: " & $ret)
        Exit
    EndIf
    Sleep($delay)
WEnd

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.