Reputation: 21
I am writing a powershell script that allows Logstash to run for a certain amount of time and then stops the program by process id. Here is a basic version of the code:
$logstashFilepath = "C:\Users\emckenzie\Downloads\logstash-5.3.2\logstash-5.3.2\bin\logstash.bat"
$logstashArguments = "-f C:\users\emckenzie\Downloads\logstash-5.3.2\logstash-5.3.2\test_loader.conf"
$logstashprocess = Start-Process -FilePath $logstashFilepath -ArgumentList $logstashArguments -PassThru
$logstashid = $logstashprocess.Id
Write-Host $logstashid
Start-Sleep -s 60
Stop-Process $logstashid
In this test case Logstash only writes from stdin to stdout. When I run this program the output looks like this:
17120
Stop-Process : Cannot find a process with the process identifier 17120.
At C:\Users\emckenzie\Documents\Loaders\testLoader.ps1:13 char:13
+ Stop-Process <<<< $logstashid
+ CategoryInfo : ObjectNotFound: (17120:Int32) [Stop-Process], ProcessCommandException
+ FullyQualifiedErrorId : NoProcessFoundForGivenId,Microsoft.PowerShell.Commands.StopProcessCommand
Is this a problem in Logstash or Powershell?
Any help would be much appreciated!
Upvotes: 1
Views: 961
Reputation: 21
I ended up using NSSM to start Logstash as a service instead, and I control NSSM through Powershell.
# You must configure testlogstash using the GUI NSSM provides
$logstashFilepath = "C:\Users\emckenzie\Downloads\nssm-2.24\nssm-
2.24\win64\nssm.exe"
$start = "start testlogstash"
$stop = "stop testlogstash"
$logstashprocess = Start-Process -FilePath $logstashFilepath -ArgumentList
$start -PassThru
Start-Sleep -s 60
Start-Process -FilePath $logstashFilepath -ArgumentList $stop
Upvotes: 1
Reputation: 1226
After reading the comments on the original post I believe there is a solution for you. If you know the program running that needs to stay running until the job is finished:
$logstashprocess = Start-Process -FilePath $logstashFilepath -ArgumentList $logstashArguments -PassThru
$logstashid = $logstashprocess.Id
$Global:IsRunning = $false
Do {
$process = Get-Process -processname "NameOfProcessHere"
If ($process){
$IsRunning = $true
}else{
$IsRunning = $false
}
} While ($IsRunning -eq $true)
Stop-Process -processname "$logstashid"
Let me know if this is helpful, or if i'm not understanding the question.
Upvotes: 0