Forum Discussion
How to write a script to start a Docker container and stop WSL safely when the container stops?
I don't quite understand the question. What are teens? (Excluding rambunctious kids.)
Cheers,
Lain
oh it's autocorrect . I was asking "what terms should I look for?"
- LainRobertsonSep 07, 2023Silver Contributor
I can't tell you what to look for with Docker, as I don't use it.
The best I can do is provide you with a hypothetical example for a scenario where the first program you start with Start-Process (in your case, this is Docker Desktop) then starts another child process of its own, indicating it's operational.
Because I don't use Docker, I have to use other programs in the example. Here's a table that translates what I'm using for illustrative purposes to what you would use.
Stage My example Your scenario Start-Process cmd.exe Docker Desktop Child process notepad.exe <something Docker launches> Example
# Launch the "main" process. $Process = Start-Process -FilePath "cmd.exe" -PassThru -ErrorAction:Stop; # Monitor for a specific child process being launched by the main process. $CheckAgain = $true; $Retries = 10; while ($CheckAgain -and ($Retries -gt 0)) { if (Get-CimInstance -ClassName "Win32_Process" -Filter "ParentProcessID=$($Process.Id) AND Name='notepad.exe'") { $CheckAgain = $false; } else { $Retries--; Start-Sleep -Seconds 2; } }
Here, we're checking for a maximum of 20 seconds ([10 retries] x [two second sleep] to see if the child process has started. If it starts earlier, then the loop will exit earlier.
But again, this is hypothetical and may not be useful for your Docker scenario at all. You'd have to do some investigating to see how Docker behaves before you can solve your challenge.
Cheers,
Lain
- ookerSep 08, 2023Copper Contributor
Actually I was broadening my topic and not talking about Docker specifically. Like, if I'm writing a program and want to register my program's events to the OS so that users can take it from there, what should I do? Only signalling via child process seems limited to me. I guess the only way to communicate with the OS is to pack all the information in the process title. I see that the Task Scheduler is something in the right way.
- LainRobertsonSep 08, 2023Silver Contributor
Okay, so setting Docker aside, there's no mandated standard.
The closest thing to a standard would be making use of the Windows event logs, where your application would write key events to either the Application log (most common as it's the oldest and therefore the most well-known log) or the Application and Services Logs node (newer, uses and different implementation and isn't widely used by vendors outside of Microsoft - though it is growing.)
Ultimately it's entirely up to the developer how they wish to inform of key events - or even to not inform at all. Again, there is no mandated standard for this.
But this is a discussion for another forum as it is entirely unrelated to PowerShell.
Cheers,
Lain