Jump to content
Sign in to follow this  
Merx

Need help on how to check for multiple processes and output to file.

Recommended Posts

Merx

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 by Merx

Share this post


Link to post
Share on other sites
JLogan3o13

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 by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
FireFox

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.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
Merx

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

Share this post


Link to post
Share on other sites
Merx

@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) -1

If ProcessExists($aProcesses[$iProcess] & ".exe") Then

FileWriteLine($hFile, _NowCalc() & ": " & @ComputerName & " Error - " & $aProcesses[$iProcess] & " failed to close")

EndIf

Next

If $blProcessesRunning Then

FileWriteLine($hFile, _NowCalc() & ": " & @ComputerName & " Succes - " & "All Processes closed succesfully")

EndIf

FileClose($hFile)

Edited by Merx

Share this post


Link to post
Share on other sites
FireFox

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.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
Merx

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 scripts

While 1

Global $hFile = FileOpen("sharetest.txt", 1)

If Not @error Then ExitLoop

WEnd

Example from text file.

Shoudl look like this

2012/10/24 12:40:47: Server1 Succes - All Processes closed succesfully

2012/10/24 12:40:48: Server2 Succes - All Processes closed succesfully

When 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 succesfully

y

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

Thanks

BR

Merx

Edited by Merx

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  

×