SAM2009
asked on
How to run a PS command for X min every Y sec?
Hi,
I run this PowerShell command in a script and lunched from different servers.
Example:
$env:computername | out-file "\\server\log\log.txt" -Append
The problem is if I lunch that script from many servers at the same time I got sometimes a msg says: The file is busy....
How can I make it try for 1 at very 5 sec or is there other way to code?
Thanks
I run this PowerShell command in a script and lunched from different servers.
Example:
$env:computername | out-file "\\server\log\log.txt" -Append
The problem is if I lunch that script from many servers at the same time I got sometimes a msg says: The file is busy....
How can I make it try for 1 at very 5 sec or is there other way to code?
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Wait for a random time between 1 and 100 milliseconds.
That line is missing parens:
Start-Sleep -Milliseconds (Get-Random -Maximum 100 -Minimum 1)
EXPERT CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
As a workaround, it will be simpler to have different log file names for each run script. is not it ?
Using sleep or any time based delays means you will always have the possibility of underestimating the duration needed. The suggestion for the retry is one approach but builds in a race condition.
A simpler approach to build into the script a method that tries to reserves a right to access before it is allowed access to the file (a built-in option should be that a file whose reservation was more than an expected duration for the script run, deals with a system shutdown after the access was reserved)
This incorporates the suggestion Michael made but in a different way with a different reason for the approach.
Depending on what it is you have as far as other resources and what it is you are doing, getting the data into a DB might be a better option if available.
A different option, is to have the servers copy their logs out to a central server that crunches the data .
A simpler approach to build into the script a method that tries to reserves a right to access before it is allowed access to the file (a built-in option should be that a file whose reservation was more than an expected duration for the script run, deals with a system shutdown after the access was reserved)
This incorporates the suggestion Michael made but in a different way with a different reason for the approach.
Depending on what it is you have as far as other resources and what it is you are doing, getting the data into a DB might be a better option if available.
A different option, is to have the servers copy their logs out to a central server that crunches the data .
ASKER
Thanks to all!
you are welcome.
ASKER