3

How do I translate the following bash statement to PowerShell?

( docker-compose -f docker-compose.yml logs -f & ) | grep -q "Initialization Complete"

The statement tails the docker logs until it finds the text "Initialization Complete", then allows the script to proceed.

I've gotten this far but am not sure how to continue script execution after the text is found.

docker-compose -f docker-compose.yml logs -f | Out-String -Stream | Select-String "Initialization Complete"
0

1 Answer 1

3

Generally, PowerShell's tail -f equivalent is Get-Content -Wait.

However, your clever combination of a Bash subshell ((...)) with a background process (&) has no direct PowerShell equivalent.

Instead, you must employ a loop to monitor the background process in PowerShell:

# Start the Docker command as a background job.
$jb = Start-Job { docker-compose -f docker-compose.yml logs -f }

# Loop until the data of interest is found.
while ($jb.HasMoreData) { 
  # Receive new data output by the background command, if any,
  # and break out of the loop once the string of interest is found.
  Receive-Job $jb -OutVariable output | 
    ForEach-Object { if ($_ -match "Initialization Complete") { break } }
  # With a stream that is kept open, $jb.HasMoreData keeps reporting $true.
  # To avoid a tight loop, we sleep a little whenever nothing was received.
  if ($null -eq $output) { Start-Sleep -Seconds 1 } 
}

# Clean up the background job, if it has completed.
if ($jb.Status -eq 'Complete') { Remove-Job $jb }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.