Jump to content
mentosan

Auto-restart of the script

Recommended Posts

mentosan

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.

Share this post


Link to post
Share on other sites
FrancescoDiMuro

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

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
mentosan

Hi Francesco,

I tried with While 1 but once the server is down, then the script will stop too.

Share this post


Link to post
Share on other sites
FrancescoDiMuro

@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

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
mentosan
Posted (edited)

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

Share this post


Link to post
Share on other sites
FrancescoDiMuro

@mentosan
Sorry, but I still don't understand what you're trying to do... Why using two functions? What is your goal?


Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
mentosan

@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.

Share this post


Link to post
Share on other sites
FrancescoDiMuro
Posted (edited)

@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

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
mentosan
_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.

Share this post


Link to post
Share on other sites
FrancescoDiMuro
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

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
mentosan

Hello Francesco,

The output is Error 2.

Share this post


Link to post
Share on other sites
mentosan

Anyone ? Some help pls

Share this post


Link to post
Share on other sites
mentosan

It is ok. I manipulated the snippet provided by Francesco, where I replaced ConsoleWrite with RunWait() function.

Thank you Francesco!

I wonder if this could be achieved using ContinueLoop ?

 

Thx

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

×