Jump to content

if run command fails then do something...is there anything like that


Recommended Posts

for example

if run(notepad.exe) fails then do something

I mean something like checking if a command really executed or not...thank you people

From the help file in the Run command section:

Return Value

Success: The PID of the process that was launched.

Failure: Returns 0 and sets @error to non-zero.

If the command fails, @error will be set to non-zero, so:

If @error then Exit ;means if error is non-zero then Exit

Closes the script if the command isn't executed. Same goes for all functions that in help file show a Return value with error set, which are almost all.

Clear?

Edited by Newb

I'm a compulsive poster. When I post something, come to read it at least 5 minutes later after the posting, because I will edit it. I edited even this signature a few minutes later after I wrote it.

Link to comment
Share on other sites

  • Moderators

Why repeat it again? :x

M23

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Why repeat it again? :P

M23

Ahah for once i got you! :x

I'm a compulsive poster. When I post something, come to read it at least 5 minutes later after the posting, because I will edit it. I edited even this signature a few minutes later after I wrote it.

Link to comment
Share on other sites

Thank you for your responses, maybe i post here my entire code. my code checks if apache is running and if its not the script automatically starts apache..if the command fails to start apache then it writes to a log file that script failed to restart apache. here is my code below: the problem here is that the script doesnt seem to check if an error occured. for example i have intentionally caused an error on apache but for some reason the if @error message doesnt work...

while 1
        
    

$apache = ProcessExists ("httpd.exe") 

    if  $apache = 0 Then 
        
         Run(@ComSpec & " /c " & 'net start wampapache', "", @SW_HIDE)
        
         sleep (500)
          
    endif
 
    if @error Then
        msgbox(4096,"","hahahahaahah")
    endif
    
WEnd
Edited by goodbyeplanet
Link to comment
Share on other sites

Thank you for your responses, maybe i post here my entire code. my code checks if apache is running and if its not the script automatically starts apache..if the command fails to start apache then it writes to a log file that script failed to restart apache. here is my code below: the problem here is that the script doesnt seem to check if an error occured. for example i have intentionally caused an error on apache but for some reason the if @error message doesnt work...

while 1
        
    

$apache = ProcessExists ("httpd.exe") 

    if  $apache = 0 Then 
        
         Run(@ComSpec & " /c " & 'net start wampapache', "", @SW_HIDE)
        
         sleep (500)
          
    endif
 
    if @error Then
        msgbox(4096,"","hahahahaahah")
    endif
    
WEnd

While 1
    $apache = ProcessExists ("httpd.exe") 

    If  @error Then
        msgbox(4096,"","hahahahaahah")
    Else
        Run(@ComSpec & " /c " & 'net start wampapache', "", @SW_HIDE)
        sleep (500)
    Endif
Wend
Edited by Newb

I'm a compulsive poster. When I post something, come to read it at least 5 minutes later after the posting, because I will edit it. I edited even this signature a few minutes later after I wrote it.

Link to comment
Share on other sites

This should just keep restarting Apache if the Process does not exist.

If the process exists, it is stuck in the inner loop

if the process does not exist, the script breaks out of the inner loop and then runs the program, and returns to the inner loop

While 1
    While 1
        If Not ProcessExists("httpd.exe") Then ExitLoop
        Sleep(500)
    WEnd
    Run(@ComSpec & " /c " & 'net start wampapache', "", @SW_HIDE)
    Sleep(500)
WEnd

Or another way

While 1
    If Not ProcessExists("httpd.exe") Then
        Do
            Run(@ComSpec & " /c " & 'net start wampapache', "", @SW_HIDE)
            Sleep(500)
        Until ProcessExists("httpd.exe")
    EndIf
    Sleep(500)
WEnd
Edited by Varian
Link to comment
Share on other sites

Taking a look at the help file might be of use here. The Run command returns the PID of the program if it's successful in running it, it returns 0 and sets the @error macro if it fails for any reason. So a simple check would be:

$Test = Run(somecommand.exe)
If $Test > 0 then
      MsgBox(0,"","Success")
Else
      MsgBox(0,"","Fail")
Endif

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Varian showed you a couple ways to do it, just log that it wasn't running. If it gets started and loops the loop should not repeat if it does start.

* Removed - doesn't appear to work tweaking Varians code to have logging

You'll stay in that loop until httpd starts. You could set a variable and try the loop x-amount of times before giving up. Even use that as a flag for the amount of times to log so it doesn't log x amount of times.

Edited by ZacUSNYR
Link to comment
Share on other sites

Taking a look at the help file might be of use here. The Run command returns the PID of the program if it's successful in running it, it returns 0 and sets the @error macro if it fails for any reason. So a simple check would be:

I am wondering, however, because he is actually running the net command, will the program actually return a value that means something to the OP. You would actually get the PID from the net command, not from the service that it is starting.
Link to comment
Share on other sites

if i follow the suggestion from BrewManNH the problem is that Run(@ComSpec & " /c " & 'net start wampapache', "", @SW_HIDE) will always be greater than 0. i dont know why but in my case thats what is happening. even if i stop the apache and worse still even if i distort my apache path so as to force a read failure, for some reason the command Run(@ComSpec & " /c " & 'net start wampapache', "", @SW_HIDE) is always greater than 0.

Edited by goodbyeplanet
Link to comment
Share on other sites

This will give you some logging

#include <Date.au3>
While 1
    If Not ProcessExists("httpd.exe") Then
        Switch FileExists('C:\Apache Start Log.txt')
            Case 1
                Local $Flag = 1
            Case 0
                Local $Flag = 2
        EndSwitch
        _WriteApacheLog('Server Detection Failure @ ' & _NowCalcDate() & ' ' & _NowTime() & @CRLF, $Flag)
        Do
            Run(@ComSpec & " /c " & 'net start wampapache', "", @SW_HIDE)
            Sleep(5000) ;increase this time for less logging or to give more time for httpd.exe to start (and possibly fail)
            If Not ProcessExists("httpd.exe") Then
                _WriteApacheLog('Failure to Start Server @ ' & _NowCalcDate() & ' ' & _NowTime() & @CRLF, $Flag)
            EndIf
        Until ProcessExists("httpd.exe")
    EndIf
    Sleep(500) ;increase this time for less logging or to give more time for httpd.exe to start (and possibly fail)
WEnd

Func _WriteApacheLog($Message, $Opt)
    Local $File = FileOpen('C:\Apache Start Log.txt', $Opt)
    FileWrite($File, $Message)
    FileClose($File)
EndFunc   ;==>_WriteApacheLog

Link to comment
Share on other sites

ok here is what i have done. Maybe if somebody tells me why the msgbox output gives a result of o on the first run of the loop then i might somehow figure out my way forward. However on the second run of the loop the msgbox displays that apache has started. why is it like this. i am expecting that the run statement will start apache then wait for 2500 and give apache time to start then if it hits the second if statement i expect that the value of $apache will change since apache would have been started already.

while 1
        
    

$apache = ProcessExists ("httpd.exe") 

    if  $apache = 0 Then 
        
        Run(@ComSpec & " /c " & 'net start wampapache', "", @SW_HIDE)
       
       sleep (2500)
        
        if $apache = 0 then

                        
            msgbox(4096,"",$apache)
          
         endif
      
    endif  
    
 
    
WEnd

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