# Exploring More Dotnet Command Line Background Processes

> There are many ways to work with `dotnet run` as a background process. Let's look at other options to DotnetBackground.

- URL: https://blog.nimblepros.com/blogs/exploring-more-dotnet-command-line-background-processes/
- Author: [Sarah Dutkiewicz](https://blog.nimblepros.com/authors/sadukie/)
- Published: 2024-04-09
- Categories: [Software Development](https://blog.nimblepros.com/categories/software-development/)
- Publisher: [NimblePros](https://blog.nimblepros.com/)

---

In [my last post](/blogs/running-dotnet-processes-in-the-background/), I wrote about wanting to run my web projects and tests in one terminal window and how I achieved it using [DotnetBackground](https://github.com/javiertuya/dotnet-background).

We received [this question from Peter Ritchie on Twitter](https://twitter.com/peterritchie/status/1776685257082687957):

> How does this compare to `start dotnet run` in Command Prompt, and `start-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:

```shell
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:

```shell
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](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/start).

In theory, I can use `/b` to run this as a background process.

Let's try this:

```shell
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.

```shell
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:

```shell
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:

```shell
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:

```shell
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 the `Start-Process`cmdlet. This alias is **not** the same as the `start` command I mentioned earlier.

So in theory, I should be able to run this command in PowerShell:

```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](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-7.4#-nonewwindow). That `-NoNewWindow` looks promising. Try this:

```powershell
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:

```powershell
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:

- [Batch script using DotnetBackground](https://github.com/NimblePros/NimblePizzaDemo/blob/main/test-all.bat)
- [Bash script](https://github.com/NimblePros/NimblePizzaDemo/blob/main/test-all.sh)
- [Command Prompt batch script](https://github.com/NimblePros/NimblePizzaDemo/blob/main/test-all-cmd.bat)
- [PowerShell script](https://github.com/NimblePros/NimblePizzaDemo/blob/main/test-all.ps1)

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](https://twitter.com/nimblepros), [BlueSky](https://bsky.app/profile/nimblepros.com/), leave a comment on [our posts on LinkedIn](https://linkedin.com/company/nimblepros), or comment on [our videos on YouTube](https://youtube.com/@NimblePros)! We appreciate the feedback and questions!
