Jump to content

Auto-restart of the script


Recommended Posts

Hi !

I have the following script, which basically will call a function as long as the Ping to a server is alive.

The problem is that once the connection to server is lost, the script will exit. My goal is to have the script running continuously, checking/keeping Ping permanently to the server until is alive again and then to restart automatically the same function.

Here is the code:

 

_WaitForIP("x.x.x.x")
Func _WaitForIP($sIP)
    Do
    Call("_some_function")
    Until Ping($sIP) <> 0
EndFunc

 

Thanks in advance for any help.

Link to comment
Share on other sites

Hi @mentosan:)
So, you're trying to check if a server is running, and you need to do this continuousely, am I right?
To do this, you could use a While 1 loop, in which you call your function, with a Sleep(), in order to check every amount of time the server status.
Another thing I feel to suggest you, is to separate the "check" function, and the loop :)

Hope it helps.

Best Regards.

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

@mentosan
I suggest you to check the return value and the @error of Ping() function, because, as the Help File says:

 

Return Value
Success: the roundtrip-time in milliseconds ( greater than 0 ). 
Failure: 0 if host is not pingable or other network errors occurred and sets the @error flag to non-zero. 
@error: 1 = Host is offline
2 = Host is unreachable
3 = Bad destination
4 = Other errors

Try to add an 

If @error Then
    MsgBox($MB_ICONERROR, "", "Error: " & @error)
EndIf

after the calling of Ping() function :)

Best Regards.

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

OK, here is more of the code I am trying to accomplish:

#include <MsgBoxConstants.au3>


_WaitForIP("192.168.0.140")
Func _WaitForIP($sIP)
    Do
    Call("_RunCmd")
    Until Ping($sIP) <> 0
    If @error Then
    MsgBox($MB_ICONERROR, "", "Error: " & @error)
    EndIf
EndFunc



_RunCmd('ping 8.8.8.8 -t')

Func _RunCmd( $command )
   Return RunWait( @ComSpec & " /C " & $command, "" )
EndFunc

 

What I am trying to do is:

- when I pull out the Ethernet with 192.168.0.140 cable, the cmd window with ping to 8.8.8.8 will be closed - which is normal behavior

- when I plug in back the Ethernet cable, 192.168.0.140 is automatically assigned. The script should make the cmd window appearing automatically showing again pinging to 8.8.8.8

 

Thanks.

PS. I am sorry If I was not clear from the beginning. 8.8.8.8 IP address is beeing used as an example.

Edited by mentosan
Link to comment
Share on other sites

@FrancescoDiMuro

 

The second function is the standard function for sending commands into terminal - this one works properly.

 

The first function is used to call the second function, using the condition as long as the ping to 192.168.0.104 is alive. 

But sometimes 192.168.0.104 is offline (ping) and when 192.168.0.104 gets alive again, I want (using the script) that the second function is called again.

Link to comment
Share on other sites

@mentosan
Running this snippet, when you unplug the Ethernet cable ( 192.168.0.104 ), which is the error displayed?

#include <AutoItConstants.au3>
#include <Misc.au3>
#include <MsgBoxConstants.au3>

Local $strWatchIP = "192.168.0.104", _
      $intPing = 0

While Not _IsPressed("1B")
    $intPing = Ping($strWatchIP)
    If @error Then
        ; MsgBox($MB_ICONERROR, "", "Error: " & @error)
        ConsoleWrite("Error: " & @error & @CRLF)
    Else
        ConsoleWrite("Ping is: " & $intPing & " ms." & @CRLF)
    EndIf
    Sleep(1000)
WEnd

Best Regards.

EDIT: Press "ESC" to exit the script.

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

_WaitForIP("192.168.0.140")
Func _WaitForIP($sIP)
    If Ping($sIP) <> 0
        Call("_RunCmd")
    Else
    ContinueLoop()
    EndIf
EndFunc

I tried the code above, using ContinueLoop, but it is not ok. After first unsuccessful Ping, the script will exit the loop

So the condition I need is, run the cmd function as long as Ping towards 192.168.0.140 is ok, else restart the loop and check each time if the Ping is ok or not.

Link to comment
Share on other sites

16 hours ago, FrancescoDiMuro said:

@mentosan
Running this snippet, when you unplug the Ethernet cable ( 192.168.0.104 ), which is the error displayed?

#include <AutoItConstants.au3>
#include <Misc.au3>
#include <MsgBoxConstants.au3>

Local $strWatchIP = "192.168.0.104", _
      $intPing = 0

While Not _IsPressed("1B")
    $intPing = Ping($strWatchIP)
    If @error Then
        ; MsgBox($MB_ICONERROR, "", "Error: " & @error)
        ConsoleWrite("Error: " & @error & @CRLF)
    Else
        ConsoleWrite("Ping is: " & $intPing & " ms." & @CRLF)
    EndIf
    Sleep(1000)
WEnd

Best Regards.

EDIT: Press "ESC" to exit the script.

@mentosan What outuput did you have running this code? :)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...