Merx Posted October 23, 2012 Share Posted October 23, 2012 (edited) Hi Guys/Gals I am very new to autoit and to scripting/programming in general and i need to create a script that can check for multiple specified processes and if they are running then write to a file. I have made a script that succesfully closes some processes, which i need to close before i can perform other remotely executed tasks on the server, but i would like a check if they are closed so i can be sure about it without having to do rdp to the remote servers and check on each one. The check could be just to look in a log file and see of they all got closed or if some failed to close. I would just need one statement for each server where i will run a different autoit script for each of them. The statement just need to be either "Date&Time all processes closed succesfully on server x" or "Date&Time some processes failed to close on server x" I use psexec to remotely execute the scripts that closes processes on the servers. My problem is how can i make my if statement check for 7 processes without having to make 7 individual if statements and can i append the result to a "log file" with a time stamp ? The below doesn't work and i guess i cant use If/Or like that, but maybe some of you can tell me the correct way to do it ? Local $file = FileOpen("sharetest.txt", 1) IF ProcessExists("process1.exe") Or ("process2.exe") Or ("process3.exe") Or ("process4.exe") Or ("process5.exe") Or ("process6.exe") Or ("process7.exe") Then FileWriteLine($file, " %Timestamp% Some processes failed to close on server x") Else FileWriteLine($file, %Timestamp %"All Processes closed succesfully on server x") EndIf Edited October 23, 2012 by Merx Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted October 23, 2012 Moderators Share Posted October 23, 2012 (edited) Hi, Merx, welcome to the forum. Try a Select statement.: Select Case ProcessExists("notepad.exe") FileWriteLine($file, $text) ContinueCase Case ProcessExists("explorer.exe") FileWriteLine($file, $text) ContinueCase EndSelect Edit: forgot to continue case. Edit: just saw you're trying to avoid individual statements. That is going to be one ugly IF line Edited October 23, 2012 by JLogan3o13 "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum! Link to comment Share on other sites More sharing options...
FireFox Posted October 23, 2012 Share Posted October 23, 2012 Hi, Welcome to the autoit forum Try this (not tested) : #include <Date.au3> Global $hFile = FileOpen("sharetest.txt", 1) Global $aProcesses[7] = ["process1","process2","process3","process4","process5","process6","process7"] Global $blProcessesRunning = True For $iProcess = 0 To UBound($aProcesses) -1 If Not ProcessExists($aProcesses[$iProcess] & ".exe") Then FileWriteLine($hFile, _NowCalc() & ": " & $aProcesses[$iProcess] & " failed to close on server x") $blProcessesRunning = False EndIf Next If $blProcessesRunning Then FileWriteLine($hFile, _NowCalc() & ": All Processes closed succesfully on server x") EndIf FileClose($hFile) You can use macros instead of _NowCalc (like @HOUR @MIN...) Br, FireFox. Link to comment Share on other sites More sharing options...
Merx Posted October 23, 2012 Author Share Posted October 23, 2012 Thanks a lot for those very fast responses. @ Firefox: Looks very good, I will try to use your suggestion first thing tomorrow when i am back at work. @ JLogan3o13: Also thanks to you, for mentioning select statements. In fact i might want to change a script i already made to make use of those. BR Merx Link to comment Share on other sites More sharing options...
Merx Posted October 24, 2012 Author Share Posted October 24, 2012 (edited) @Firefox: Your suggestions worked very well, just did some minor changes, because i changed my mind regarding how to get the computername. instead of typing it in every script i use @CommputerName instead.One ting that still troubles me that with several servers running a script like this and writing to the same text log, i risk the file being accessed by several scripts at the same time and therefore the file might be locked while some other scripts try to write to it.Is there a way to check for this, like checking if file is writeable and if not then sleep for x seconds and try again, keep doing until file was writeable and was written to with succes. I guess i risk this keeping a loop then if the log file should hang. How could i make this ? Another way would be to keep a seperate text log for each server where the script is run at and then make a new script that read all the text files and extracts the content to one single file. After it has read the different text log files then flush them to avoid getting old data extracted to the main log file ?If possible i would like the scripts to write to the same log file, so if someone has experience or suggestions to how to make this work i would be happy to know.Current script from firefox with a little change.#include <Date.au3>Global $hFile = FileOpen("sharetest.txt", 1)Global $aProcesses[7] = ["process1","process2","process3","process4","process5","process6","process7"]Global $blProcessesRunning = True For $iProcess = 0 To UBound($aProcesses) -1If ProcessExists($aProcesses[$iProcess] & ".exe") ThenFileWriteLine($hFile, _NowCalc() & ": " & @ComputerName & " Error - " & $aProcesses[$iProcess] & " failed to close")EndIfNextIf $blProcessesRunning ThenFileWriteLine($hFile, _NowCalc() & ": " & @ComputerName & " Succes - " & "All Processes closed succesfully")EndIfFileClose($hFile) Edited October 24, 2012 by Merx Link to comment Share on other sites More sharing options...
FireFox Posted October 24, 2012 Share Posted October 24, 2012 One ting that still troubles me that with several servers running a script like this and writing to the same text log, i risk the file being accessed by several scripts at the same time and therefore the file might be locked while some other scripts try to write to it. There is a little chance to happen, and if it's the case then you can do like this : While 1 Global $hFile = FileOpen("sharetest.txt", 1) If Not @error Then ExitLoop WEnd You can also not open the file and only use FileWriteToLine with the path of the file and not the handle for the 1st parameter. Another way would be to keep a seperate text log for each server where the script is run at and then make a new script that read all the text files and extracts the content to one single file. After it has read the different text log files then flush them to avoid getting old data extracted to the main log file ? Yes you can create all logs in the same folder, then searching for each one with the function _FileListToArray Br, FireFox. Link to comment Share on other sites More sharing options...
Merx Posted October 24, 2012 Author Share Posted October 24, 2012 (edited) I tryed the below code at the beginning of 2 identical scripts and ran them at the same time. I get only 1 of them doing input to the text file. The other writes "y" on the next line.I put this at the top of the scriptsWhile 1Global $hFile = FileOpen("sharetest.txt", 1) If Not @error Then ExitLoop WEndExample from text file.Shoudl look like this2012/10/24 12:40:47: Server1 Succes - All Processes closed succesfully2012/10/24 12:40:48: Server2 Succes - All Processes closed succesfullyWhen run at the same time ( made a copy of the script and ran the original and the copy at the same time on the same machine)2012/10/24 12:42:02: server1 Succes - All Processes closed succesfullyyI dont know what the risk are when up to 20 identical scripts are started on the same time on 20 different servers, but i would like to prevent the posibility for this to happen.Regarding the other method you wrote about, to not open the file and only use FileWriteLine with the path of the file and not the handle.This worked for me like 4/5 times, but still a few times when i ran the scripts at the same time on the same machine, it gave me a line with just a few characters. However i think the risk is small and that this solution will do the job. I will try and use it for the first version atleast and see what happens when i execute it on all servers a few times.If there should be an issue with it then ill try to use the last solution and use seperate text file for each server and then extract data from them onto one main text log... But i hope that wont be nessesary.ThanksBRMerx Edited October 24, 2012 by Merx 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