In my last post, I wrote about wanting to run my web projects and tests in one terminal window and how I achieved it using DotnetBackground.
We received this question from Peter Ritchie on Twitter:
How does this compare to
start dotnet run
in Command Prompt, andstart-process -FilePath dotnet -ArgumentList 'run'
in PowerShell?
Let’s see if we can use these commands to also accomplish our goals of running the web apps and tests in one terminal window.
DotnetBackground
Let’s recap with what we ended up having to do in DotnetBackground. This is what I had to do to start the dotnet run
as a background process:
DotnetBackground run --project .\NimblePizza.Blazor\NimblePizza.Blazor.csproj --launch-profile https
Then, I could run dotnet test
.
Finally, I could stop the background processes with:
DotnetBackground kill
start
Command in the Command Prompt
The first command Peter asked about is the start
command in the Command Prompt. Initially, I tried start dotnet run
with my parameters from above, but it opened in another terminal window. Time to look at the start
documentation.
In theory, I can use /b
to run this as a background process.
Let’s try this:
start /b dotnet run --project .\NimblePizza.Blazor\NimblePizza.Blazor.csproj --launch-profile https
This loaded the site. However, I could press [Enter]
to get back to the command prompt. Let’s try dotnet test
.
Tests passed! Next up… how do we identify the dotnet process and stop it?
Since we don’t know the process ID, we can use taskkill
with the executable name to kill the process.
taskkill /IM dotnet.exe
If it fails to terminate, there may be a good reason for it. The error messages tend to be descriptive in what went wrong. When I ran this the first time, it told me that the process needed to be stopped forcefully (with /F option). So I ended up running this:
taskkill /IM dotnet.exe /F
Now what if you wanted to find the process ID? Use tasklist
to see all current tasks. You can use /M
and do a wildcard search of what’s running. So I found my process ID (PID) using:
tasklist /M dotnet*
In my results, the dotnet
executable was running under PID 18392. So now, if I want to kill it, I can do so with the following command:
taskkill /PID 18932 /F
Start-Process
in PowerShell
The other part of Peter’s question was around start-process -FilePath dotnet -ArgumentList 'run'
. Let’s see how we can accomplish this in PowerShell!
Note: PowerShell has an alias of
start
for theStart-Process
cmdlet. This alias is not the same as thestart
command I mentioned earlier.
So in theory, I should be able to run this command in PowerShell:
Start-Process -FilePath dotnet -ArgumentList "run --project .\NimblePizza.Blazor\NimblePizza.Blazor.csproj --launch-profile https"
However, this starts another terminal window. Let’s consult the Start-Process documentation. That -NoNewWindow
looks promising. Try this:
Start-Process -NoNewWindow -FilePath dotnet -ArgumentList "run --project .\NimblePizza.Blazor\NimblePizza.Blazor.csproj --launch-profile https"
When you press [Enter]
, it will return you to a command prompt. From here, you can run dotnet test
.
Once you’re done with those dotnet processes, you can stop them with this command:
Stop-Process -Name dotnet
Conclusion
In the past two blog posts, I covered how to run your web projects and tests in one terminal window, rather than juggling multiple terminal windows or multiple windows between a terminal and Visual Studio. These scripts may give you ideas on how to automate these processes for CI/CD pipelines as well. These files are all available in our NimblePizzaDemo repo:
Thanks, Peter Ritchie, for the question!
If you have questions for us that you would like to see answered in a future blog post, reach out to us on Twitter, BlueSky, leave a comment on our posts on LinkedIn, or comment on our videos on YouTube! We appreciate the feedback and questions!