goodbyeplanet Posted January 13, 2011 Share Posted January 13, 2011 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 Link to comment Share on other sites More sharing options...
Newb Posted January 13, 2011 Share Posted January 13, 2011 (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 January 13, 2011 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 More sharing options...
Moderators Melba23 Posted January 13, 2011 Moderators Share Posted January 13, 2011 (edited) Why repeat it again? M23 Edited January 13, 2011 by Melba23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
marko29 Posted January 13, 2011 Share Posted January 13, 2011 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 ,,,, Link to comment Share on other sites More sharing options...
marko29 Posted January 13, 2011 Share Posted January 13, 2011 (edited) Melba23 since you are all knowing help me with my run command issue!!!! btw if you mean me(repeating) i guess i was typing in the same time as you lol Edited January 13, 2011 by marko29 Link to comment Share on other sites More sharing options...
Newb Posted January 13, 2011 Share Posted January 13, 2011 Why repeat it again? M23Ahah for once i got you! 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 More sharing options...
goodbyeplanet Posted January 13, 2011 Author Share Posted January 13, 2011 (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 January 13, 2011 by goodbyeplanet Link to comment Share on other sites More sharing options...
Newb Posted January 13, 2011 Share Posted January 13, 2011 (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 January 13, 2011 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 More sharing options...
goodbyeplanet Posted January 13, 2011 Author Share Posted January 13, 2011 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 now that script doesnt work, i tried it before Link to comment Share on other sites More sharing options...
Varian Posted January 13, 2011 Share Posted January 13, 2011 (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 wayWhile 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 January 13, 2011 by Varian Link to comment Share on other sites More sharing options...
goodbyeplanet Posted January 13, 2011 Author Share Posted January 13, 2011 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. Link to comment Share on other sites More sharing options...
BrewManNH Posted January 13, 2011 Share Posted January 13, 2011 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 GudeHow 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 More sharing options...
ZacUSNYR Posted January 13, 2011 Share Posted January 13, 2011 (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 January 13, 2011 by ZacUSNYR Link to comment Share on other sites More sharing options...
Varian Posted January 13, 2011 Share Posted January 13, 2011 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 More sharing options...
ZacUSNYR Posted January 13, 2011 Share Posted January 13, 2011 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. Link to comment Share on other sites More sharing options...
goodbyeplanet Posted January 13, 2011 Author Share Posted January 13, 2011 (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 January 13, 2011 by goodbyeplanet Link to comment Share on other sites More sharing options...
ZacUSNYR Posted January 13, 2011 Share Posted January 13, 2011 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. Link to comment Share on other sites More sharing options...
ZacUSNYR Posted January 13, 2011 Share Posted January 13, 2011 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? Link to comment Share on other sites More sharing options...
Varian Posted January 13, 2011 Share Posted January 13, 2011 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 More sharing options...
goodbyeplanet Posted January 13, 2011 Author Share Posted January 13, 2011 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now