Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell run two processes in parallel and display combined stdout in the terminal

Tags:

powershell

I'd like to get some help putting together a PowerShell script that starts two processes (in my case two servers that monitor file changes and report, writing stuff to stdout continuously) and mixes their respective outputs into one terminal window?

I am not interested in writing into files. I want to monitor the two intertwined output streams in the same PowerShell terminal window. I know that at points there will be mix of "unfinished" chunks from one process intertwined with the same from the other etc., but I don't mind that.

Image these two scripts as inputs:

left.ps1

echo 0
Start-Sleep 1
echo 1
Start-Sleep 2
echo 2
Start-Sleep 3
echo 3
Start-Sleep 4
echo 4
Start-Sleep 5
echo "done numbers"

right.ps1

echo A
Start-Sleep 1
echo B
Start-Sleep 2
echo C
Start-Sleep 3
echo D
Start-Sleep 4
echo E
Start-Sleep 5
echo "done letters"

The desired outputs is:

0
A
1
B
2
C
3
D
4
E
done numbers
done letters

It would be nice having the process die when both of the started processes die and also to kill the started processes upon ^C, but neither is required.

I am using Windows so a Windows-only answer is fine. I do not want to use tmux or screen if possible. (I'm not interested in Cygwin and also I do not want two columns but a mix of stdouts.) A WSL solution would also be interesting.

like image 282
Tomáš Hübelbauer Avatar asked Oct 30 '17 18:10

Tomáš Hübelbauer


People also ask

Can I run 2 PowerShell scripts in parallel?

Parallel foreach (PowerShell 7.0)You can run all scripts in parallel for each piped input object. If your script is crunching a lot of data over a significant period of time and if the machine you are running on has multiple cores that can host the script block threads.

How do I run two scripts simultaneously in PowerShell?

To execute multiple commands in Windows PowerShell (a scripting language of Microsoft Windows), simply use a semicolon.

How do I combine two PowerShell commands?

Chaining multiple PowerShell commands in one line However, if you want to do run multiple unrelated commands on the same line, you can use the firstcommand; secondcommand method. In this case, the second command gets executed even if the first command fails.

Can PowerShell multithread?

Starting in PowerShell 7.0, the ability to work in multiple threads simultaneously is possible using the Parallel parameter in the Foreach-Object cmdlet. Monitoring the progress of these threads can be a challenge though. Normally, you can monitor the progress of a process using Write-Progress.


1 Answers

You can use Start-Process cmdlet with -NoNewWindow switch to start new independent console application attached to the same console window:

Start-Process -NoNewWindow process1 arguments1
Start-Process -NoNewWindow process2 arguments2

If you want to start PowerShell script this way, then you need to start new PowerShell process for it:

Start-Process -NoNewWindow powershell '-File script1.ps1'
Start-Process -NoNewWindow powershell '-File script2.ps1'
like image 146
user4003407 Avatar answered Oct 13 '22 00:10

user4003407