Sign in to follow this  
Followers 0
goodbyeplanet

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

32 posts in this topic

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Why repeat it again? :x

M23

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

Return Value

Success: The PID of the process that was launched.

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

$pid = run(notepad.exe)

if $pid = 0 Then

....

or simply add bellow your code

if @error Then

,,,,

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Melba23 since you are all knowing help me with my run command issue!!!! :x

btw if you mean me(repeating) i guess i was typing in the same time as you lol

Edited by marko29

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

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.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

yes Varian thank you but my main issue is not about restarting apache but its about logging the results if the run statement fails to start apache. so this means that i am still trying to figure out making a condition on the run statement itself.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

That's what I think is the problem here varian. I'm testing the the script and once I realized i'm not failing the ProcessExists (due to having httpd running, DOH). That the Run command is the issue.

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

That's because the machine *IS* running cmd.exe and you're getting that PID.

I can think of a way to gather the outcome of net start and do exactly what you're looking to do but I don't know how proper it would be.. heh.

Share this post


Link to post
Share on other sites

Let's try thinking about this a different way. Your goal of a program like this is...

1. Check to make sure HTTPD is running.

2. If not running - start it.

3. If HTTPD could not start, notify/log.

Correct?

Share this post


Link to post
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

Share this post


Link to post
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

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  
Followers 0